QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#16250. The Way Home

Statistics

In 2046, the urban rail transit construction in OI City was finally completed. Due to meticulous early planning, the completed rail network consists of $2n$ subway lines, forming a grid of $n$ vertical and $n$ horizontal lines. As shown in the figure below, each of these $2n$ lines contains $n$ stations, and every station is located at the intersection of one horizontal and one vertical line.

Due to construction costs, not every station allows for in-station transfers. There are $m$ subway stations where in-station transfers are possible. In the figure below, stations marked with a square are transfer stations. It is known that traveling between two adjacent stations on a line takes $2$ minutes, and an in-station transfer takes $1$ minute. Serenade wants to know the minimum time required to travel from school to home without exiting the station network (waiting time for trains is ignored).

Input

The first line contains two integers $n$ and $m$.

The next $m$ lines each contain two integers $x$ and $y$, indicating that the intersection of the $x$-th horizontal line and the $y$-th vertical line is a transfer station.

The last line contains four integers $x_1, y_1, x_2, y_2$. This indicates that Serenade boards the train at the intersection of the $x_1$-th horizontal line and the $y_1$-th vertical line, and gets off at the intersection of the $x_2$-th horizontal line and the $y_2$-th vertical line.

Output

Output a single line containing the minimum time required for Serenade to get home by choosing the lines reasonably. If Serenade cannot get home without exiting the station network, output -1.

Examples

Input 1

2 1
1 2
1 1 2 2

Output 1

5

Input 2

6 9
2 1
2 5
3 2
4 4
5 2
5 6
6 1
6 3
6 4
1 1 4 6

Output 2

27

Input 3

6 10
2 1
2 5
3 2
4 4
5 2
5 6
6 1
6 3
6 4
6 6
1 1 4 6

Output 3

26

Subtasks

For $30\%$ of the data, $n\le 50$, $m\le 1\,000$;

For $60\%$ of the data, $n\le 500$, $m\le 2\,000$;

For $100\%$ of the data, $n\le 20\,000$, $m\le 100\,000$;

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.