Obstacle Course

Time limit2sMemory limit128 MB

Summary
Find the minimum number of one-second straight-line moves for a puck starting at rest at the origin, where each flick changes a velocity component by 1 m/s up to 7, avoiding stick obstacles and ending exactly on the target point.
Level

Medium7 of 10

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

Problem

You have a puck that slides freely on ice. Once every second you may flick the puck from one of its four sides — north, south, east, or west. A flick from a given side pushes the puck toward the opposite side, changing that component of its velocity by 11 m/s. Use a coordinate system where east is the +x+x direction and north is the +y+y direction, so a flick from the west increases the eastward velocity, a flick from the south increases the northward velocity, and so on. At the start the puck is at (0,0)(0,0) and is at rest.

Movement example. Suppose we flick the puck 55 times: from the west, from the south, from the west again, and then twice from the north.

  • During second 11 the puck moves 11 m east and reaches (1,0)(1,0).
  • During second 22 it moves 11 m east and 11 m north, reaching (2,1)(2,1).
  • During second 33 it moves 22 m east and 11 m north, reaching (4,2)(4,2).
  • During second 44 it moves 22 m east, reaching (6,2)(6,2).
  • During second 55 it moves 22 m east and 11 m south, reaching (8,1)(8,1).

Each second the puck travels in a straight line, directly from its starting point to its ending point for that second. You are not required to flick; if you do not, the puck keeps the same direction and speed. The maximum speed is 77 m/s along the east–west axis and 77 m/s along the north–south axis. For instance, if the puck is currently moving 77 m/s west and 44 m/s north, it can no longer be flicked from the east (that would push it past 77 m/s west), but it can still be flicked from the other sides.

Obstacles are also placed on the ice. Each obstacle is a stick lying flat that connects two points with integer coordinates. Your goal is to move the puck to a given target point as quickly as possible without touching any obstacle. For simplicity, both the sticks and the puck are one-dimensional, and they touch if and only if they occupy exactly the same point. The puck touches an obstacle if it finishes a second's path at a point occupied by an obstacle, or if it passes through such a point on the way.

The puck does not have to come to rest at the target, but it must finish one of its one-second straight-line moves exactly at the target point.

Input

The first line contains 33 integers: the coordinates of the target point (xx then yy) and the number of obstacles NN (at most 100100). Each of the next NN lines contains four integers, the coordinates of the two endpoints of one obstacle stick. Every input coordinate is an integer in the range −10…10-10 \ldots 10.

Output

Output exactly one integer: the minimum number of seconds in which the target can be reached. If the target cannot be reached, output −1-1.

Hint

One optimal solution for the illustrated case flicks the puck from these sides in order: east, south, west, west, south.

Examples3

  1. Example 1

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

    Input
    1 0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    0 5 0
    
    Expected output
    3