This page is still under construction.

Parts of this page are still being built. What you see may change.

Obstacle Course

Time limit1sMemory limit1024 MB

Summary
Find the minimum number of seconds to steer a sliding puck to a target on an ice rink, accelerating per second while avoiding integer-coordinate obstacles.
Level

Hard8 of 10

Topics
BFS, Simulation, Implementation, Greedy
Solved
No attempts yet

Problem

You have a puck that slides across an ice rink. Once per second you may hit the puck from one side: from the north, south, east, or west. Hitting the puck from a side increases its speed in the opposite direction by 11 meter per second. For example, a hit from the west increases the eastward speed by 11 m/s, and a hit from the south increases the northward speed by 11 m/s. Initially the puck rests at coordinates (0,0)(0, 0).

Example of movement: we hit the puck five times: from the west, from the south, from the west again, and then twice from the north. During the first second the puck moves 11 meter east and reaches (1,0)(1, 0). During the second second it moves 11 meter east and 11 meter north, reaching (2,1)(2, 1). During the third second it moves 22 meters east and 11 meter north, reaching (4,2)(4, 2). During the fourth second it moves 22 meters east, reaching (6,2)(6, 2). During the fifth second it moves 22 meters east and 11 meter south, reaching (8,1)(8, 1).

During each second the puck moves in a straight line from its start point to its end point. You are not required to hit the puck; if you do not, its direction and speed stay the same. The puck's maximum speed is 77 m/s in each of the east-west and north-south directions. For example, if the puck is currently moving 77 m/s west and 44 m/s north, it can no longer be hit from the east, but it can still be hit from the other directions.

Obstacles are also placed on the ice. Each obstacle is an upright stick at a point with integer coordinates.

The goal is to move the puck to a given destination as quickly as possible without touching any obstacle. For simplicity, assume that both the sticks and the puck are dimensionless points, and that they touch each other if and only if they are at exactly the same point. The puck is said to touch an obstacle if it ends a second's path at a point that holds an obstacle, or if its path passes through such a point.

The puck does not need to stop at the destination, but it must finish that second's path exactly at the destination point.

Input

The first line contains three integers: the two coordinates of the destination and the number of obstacles NN (at most 100100). Each of the next NN lines contains two integers giving the position of one obstacle. All coordinates in the input are integers from −10-10 to 1010.

Output

Output exactly one integer: the minimum number of seconds needed to reach the destination. If the destination cannot be reached, output −1-1.

Hint

For the first sample, you can reach the destination (0,5)(0, 5) in 55 seconds by hitting the puck each second as follows: from the west, from the east, from the south, from the south, from the east.

Examples3

  1. Example 1

    Input
    0 5 5
    -1 0
    -1 4
    0 4
    1 4
    2 3
    
    Expected output
    5
    
  2. Example 2

    Input
    1 0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    2 2 1
    2 2
    
    Expected output
    -1