Longest Shortest Paths

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

Consider nn axis-aligned rectangles and two vertical segments SS and TT in the plane. We assume that all corners of the rectangles and segments are in integer coordinates. We also assume that the rectangles and segments are disjoint each other, that is, no two of them intersect each other and no two of them share a boundary point. For a point pp in SS and a point qq in TT, a path between pp and qq is a chain consisting of horizontal or vertical segments that connects pp and qq and does not intersect the interiors of the rectangles. The length of a path is the sum of the lengths of segments in the path. Thus, a shortest path between pp and qq is one whose length is the smallest among all paths between pp and qq.

For every pair of a point in SS and a point in TT, there is a shortest path between them. Let d(p,q)d(p, q) denote the length of a shortest path between a point pp in SS and a point qq in TT. Our goal is to compute the length of the longest path among all shortest paths connecting a point in SS and a point in TT, that is, max_pSmax_qTd(p,q)\displaystyle\max\_{p ∈ S}\max\_{q ∈ T}d(p,q).

(a)(b)

For example, consider the figures above. Figure (a) shows two vertical segments SS and TT, and no rectangle in the plane. Every shortest path between a point in SS and a point in TT has length at most 99. Since d(s,t)=9d(s, t) = 9, we have max_pSmax_qTd(p,q)=d(s,t)=9\displaystyle\max\_{p ∈ S}\max\_{q ∈ T}d(p,q) = d(s, t) = 9 for this example.

Figure (b) shows an axis-aligned rectangle AA and two vertical segments SS and TT in the plane. There are two shortest paths between a point ss in SS and a point tt in TT, one in red color and one in blue color. Then d(s,t)=11d(s, t) = 11. Observe that every shortest path between a point in SS and a point in TT has length smaller than or equal to 1111. Thus, we have max_pSmax_qTd(p,q)=d(s,t)=11\displaystyle\max\_{p ∈ S}\max\_{q ∈ T}d(p,q) = d(s, t) = 11 for this example.

Given nn axis-aligned rectangles and two vertical segments SS and TT that do not intersect each other, write a program to compute the length of the longest path among all shortest paths connecting a point in SS and a point in TT.

입력

Your program is to read from standard input. The input starts with a line containing six integers. The first three integers represent the xx-coordinate and the two yy-coordinates of the endpoints of the vertical segment SS, and the last three integers represent the xx-coordinate and the two yy-coordinates of the endpoints of the vertical segment TT.

The next line contains an integer nn (0n5,0000 ≤ n ≤ 5\\,000), where nn is the number of axis-aligned rectangles. The rectangles are numbered from 11 to nn. In the following nn lines, the ii-th line contains four nonnegative integers. The first two integers represent the xx-coordinate and yy-coordinate of the top-left corner of the rectangle ii, and the last two integers represent the xx-coordinate and yy-coordinate of the bottom-right corner of the rectangle ii.

All the coordinate values of endpoints of SS and TT, and two corners of the rectangles are nonnegative integers no more than 10810^8.

출력

Your program is to write to standard output. Print exactly one line. The line should contain the length of the longest path among all shortest paths between a point a point in SS and a point in TT.

Sample input 1 corresponds to the case of Figure (a), and sample input 2 corresponds to the case of Figure (b).