#P3831. [SHOI2012] 回家的路

[SHOI2012] 回家的路

Description

In 2046, the urban rail transit construction of OI City was finally completed. Thanks to meticulous planning, the completed rail network consists of 2n2n subway lines, forming a grid with nn vertical and nn horizontal lines. As shown in the figure below, each of these 2n2n lines contains nn stations, and each station lies at the intersection of one vertical line and one horizontal line.

For cost reasons, not every station allows in-station transfers. There are mm stations where in-station transfer is possible; in the figure below, stations marked with squares are transfer stations. It is known that traveling one stop takes 22 minutes, and an in-station transfer requires 11 minute of walking. Serenade wants to know, without exiting the station mid-journey, the minimum time needed to get home from school (waiting time is ignored).

Input Format

The first line contains two integers n,mn, m.

Each of the next mm lines contains two integers x,yx, y, indicating that the intersection of the xx-th horizontal line and the yy-th vertical line is an in-station transfer station.

The next line contains four integers x1,y1,x2,y2x_1, y_1, x_2, y_2. They mean that Serenade boards at the intersection of the x1x_1-th horizontal line and the y1y_1-th vertical line, and alights at the intersection of the x2x_2-th horizontal line and the y2y_2-th vertical line.

Output Format

Output one line: the minimum time for Serenade to get home with an optimal choice of lines. If Serenade cannot get home without exiting the station to transfer, output -1.

2 1
1 2
1 1 2 2
5
6 9
2 1
2 5
3 2
4 4
5 2
5 6
6 1
6 3
6 4
1 1 4 6
27
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
26

Hint

  • For 30%30\% of the testdata, n50,m1000n \le 50, m \le 1000.
  • For 60%60\% of the testdata, n500,m2000n \le 500, m \le 2000.
  • For 100%100\% of the testdata, n20000,m100000n \le 20000, m \le 100000.

Translated by ChatGPT 5