Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city.
Let’s represent the city as an area of n×n square blocks. Yura needs to move from the block with coordinates (sx,sy) to the block with coordinates (fx,fy). In one minute Yura can move to any neighboring by side block; in other words, he can move in four directions. Also, there are m instant-movement locations in the city. Their coordinates are known to you and Yura. Yura can move to an instant-movement location in no time if he is located in a block with the same coordinate x or with the same coordinate y as the location.
Help Yura to find the smallest time needed to get home.
Input
The first line contains two integers n and m — the size of the city and the number of instant-movement locations (1≤n≤109,0≤m≤105).
The next line contains four integers sxsyfxfy — the coordinates of Yura’s initial position and the coordinates of his home (1≤sx,sy,fx,fy≤n).
Each of the next m lines contains two integers xiyi — coordinates of the i-th instant-movement location (1≤xi,yi≤n).
Output
In the only line print the minimum time required to get home.
Examples
input
1 2 3 4 5
5 3 1 1 5 5 1 2 4 1 3 3
output
1
5
input
1 2 3 4 5 6 7
84 5 67 59 41 2 39 56 7 2 15 3 74 18 22 7
output
1
42
Note
In the first example Yura needs to reach (5,5) from (1,1). He can do that in 5 minutes by first using the second instant-movement location (because its y coordinate is equal to Yura’s y coordinate), and then walking (4,1)→(4,2)→(4,3)→(5,3)→(5,4)→(5,5).
Translation
给定一个 n×n 的网格地图,和起点 (sx,sy),终点 (fx,fy),人只能向上下左右四个方向走,求起点到终点需要的最短时间,定义走一步时间为 1。给了m 个瞬移点,如果当前所在位置的 x 坐标或 y 坐标等于某个瞬移点的 x 坐标或 y 坐标,那么就可以瞬移到这个点。
Idea
如果我们定义起点也为一个瞬移点,那么可以断定:走后的几步,一定会从某个瞬移点徒步走到 (fx,fy)。
显然,瞬移点的数量为 m+1 个,两个瞬移点 (x1,y1) 和 (x2,y2) 之间的距离为 min(x1−x2,y1−y2)。我们可以在瞬移点两两之间建边,然后求最短路,那么就有 (m+1)×(m+2) 条边,这在时间和空间上都是不允许的。
我们需要优化建边。显然,从一个瞬移点 (x1,y1) 走向另一个瞬移点 (x2,y2),可以有两种较优的方案:从 (x1,y1) 走到 (x2,y1),称之为从 x 方向靠近;从 (x1,y2) 走到 (x1,y2) 成为从 y 方向靠近。如果是从 x 方向靠近,可以先到 x 坐标比 x2 小且最大的瞬移点,或 x 坐标大于 x2 且最小的瞬移点,再走到 (x2,y1),这样走的就是 (x1,y1) 到 (x2,y2) 的最短路径;若从 y 方向靠近,同理。
对于一个瞬移点 (x,y),直接向 (x1,yunknown) 和 (x2,yunknown) 连边。x1 是满足 x1≤x 的最大的瞬移点(除自身外)横坐标,x2 是满足 x2≥x 的最大的瞬移点(除自身外)坐标;对于 y 坐标,同理建边。
用 Dijkstra 算法即可求得起点到各个瞬移点的最短距离,最后枚举每个瞬移点作为最后直接走到 (fx,fy) 的那个瞬移点并更新答案。边的规模是 4×(m+1),时间复杂度 O(mlogm)