
The cities of the future will be laid out in perfect grids. Nobody will drive or take the bus. Transit problems will have been solved once and for all by placing teleportation devices (TDs) at intersections. Pedestrians entering an intersection containing a TD will have the option of continuing through the intersection as normal or using an app on their phone to trigger the TD and instantly teleport themselves to a new intersection. The TDs will be linked in pairs as shown below. Either TD in the pair can be used to instantly teleport a pedestrian to the other TD.
Intersections are identified using a pair of integers representing the horizontal street number (numbered North to South starting at ) and then the vertical street number (numbered West to East starting at
).
Suppose a future pedestrian wants to go from the point marked
to the point marked
on the map shown to the right. She could use the TD at
to instantly teleport to
. If she does that, her total distance will be
blocks: she walks
blocks from
to
then
from
to
. If she takes the TD at
, she can shorten her route to
blocks. But there is also a combination of
TDs that can get her to
in
blocks. Can you find it?
The input will contain test cases. The first line of each test case consists of a pair of numbers
and
which give the number of horizontal and vertical streets in a future city
. This is followed by a line with two integers
and
representing the start location of a pedestrian and another line with two integers
and
representing their finishing location
. This is followed by a line with a single integer
which gives the number of pairs of TDs
and this is followed by
lines, each containing four integers
,
,
and
representing the locations of the two TDs in each pair. Each intersection can hold only one TD.
Your job is to find the shortest path between the pedestrian's start and finish locations and report the number of city blocks the pedestrian will have to walk. The path can make use of any number of teleportation devices.
Note that the data set below contains only test cases, but the real data set will contain
.
Sample Input
8 9
7 0
0 8
6
1 1 6 6
4 1 4 7
6 1 2 5
2 3 6 2
1 8 7 5
2 4 1 7
5 4
4 0
0 3
2
2 1 0 3
4 1 2 2
1000 1000
5 2
10 970
14
0 3 4 5
1 1 13 13
1 2 2 3
7 7 9 9
1 70 70 1
9 1 1 12
2 2 3 3
3 7 2 900
3 900 5 95
3 90 5 100
4 5 5 4
6 7 8 9
9 9 8 8
9 9 5 3
Sample Output
5
2
83
Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org
Comments
The last two lines of the sample input both contain a teleporter at
. Is the previous one removed in this case, or are both stored at that coordinate?
Edit: Test cases are weak, so it doesn't matter whether or store all of them or take the new one.