Lasers and Mirrors

Given a laser, a barn, and up to 100,000 posts, find the fewest posts to mount mirrors on so the beam travels from laser to barn.

Medium7GraphBFSHash mapNo attempts yetTime limit2sMemory limit512 MB

Problem

The cows are putting on another laser light show.

The laser they got for this show is so large that they cannot move it from the spot where it was delivered. They want to send its beam to the barn on the other side of the farm. On the map of the farm the laser and the barn are points in the plane. The cows aim the laser so that the beam leaves horizontally or vertically, parallel to the x axis or the y axis. They then bounce the beam off a few mirrors to bring it to the barn.

The farm has NN fence posts (1N100,0001 \le N \le 100{,}000) standing at distinct points. No post stands where the laser or the barn is. The cows can mount a mirror on any posts they like. A post with no mirror lets the beam pass straight over it without changing direction. A post with a mirror holds it diagonally, as / or \, so it turns a horizontal beam into a vertical one and a vertical beam into a horizontal one. The cows choose the orientation of each mirror.

Compute the smallest number of mirrors that brings the beam from the laser to the barn.

Input

The first line contains five space separated integers NN, xLx_L, yLy_L, xBx_B, yBy_B, where (xL,yL)(x_L, y_L) is the location of the laser and (xB,yB)(x_B, y_B) is the location of the barn.

Each of the next NN lines contains the coordinates xx and yy of one fence post. Every coordinate is an integer between 00 and 1,000,000,0001{,}000{,}000{,}000, inclusive. The NN posts are at distinct points, and none of them is at the laser or at the barn.

Output

Print the smallest number of mirrors needed to send the beam to the barn. If no arrangement of mirrors sends the beam to the barn, print -1.