Walk

Biaoge is planning to walk to amusement park. The city he lives can be abstracted as a 2D plane. Biaoge is at (x1,y1) and the amusement park is at (x2,y2). There are also some rectangle buildings. Biaoge can only walk parallel to the coordinate axis. Of course Biaoge can’t walk across the buildings. What’s the minimum number of turns Biaoge need to make? As the figure above shows, there are 4 buildings and Biaoge need to make at least 3 turns to reach the amusement park (before walking he can chose a direction freely). It is guaranteed that all the buildings are parallel to the coordination axis. Buildings may contact but overlapping is impossible. The amusement park and Biaoge’s initial positions will not contact or inside any building. Input There are multiple test case. Each test case contains several lines. The first line contains 4 integers x1, y1, x2, y2 indicating the coordinate of Biaoge and amusement park. The second line contains one integer N (0 ≤ N ≤ 50), indicating the number of buildings. Then N lines follows, each contains 4 integer x1, y1, x2, y2, indicating the coordinates of two opposite vertices of the building. Input ends with ‘0 0 0 0’, you should not process it. All numbers in the input range from −108 to 108. Output For each test case, output the number of least turns in a single line. If Biaoge can’t reach the amusement park, output -1 instead. Hint: In the first case, Biaoge can walk along the side of building, and no turn needed. In the second case, two buildings block the direct way and Biaoge need to make 2 turns at least.

2/2 Sample Input 0 0 0 10 1 0558 0 0 0 10 2 0558 -2 1 0 5 0000 Sample Output 0 2