#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 subway lines, forming a grid with vertical and horizontal lines. As shown in the figure below, each of these lines contains 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 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 minutes, and an in-station transfer requires 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 .
Each of the next lines contains two integers , indicating that the intersection of the -th horizontal line and the -th vertical line is an in-station transfer station.
The next line contains four integers . They mean that Serenade boards at the intersection of the -th horizontal line and the -th vertical line, and alights at the intersection of the -th horizontal line and the -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 of the testdata, .
- For of the testdata, .
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号