## Mock CCC '19 Contest 1 S3 - Pusheen Eats Tuna Sashimi and Tuna Nigiri

View as PDF

Points: 12 (partial)
Time limit: 1.0s
Java 1.8s
Memory limit: 256M

Problem type

Pusheen has been dreaming about tuna sashimi! She has decided that she needs to eat more tuna in her life, so she decides to visit her favourite restaurant to eat tuna sashimi and tuna nigiri.

Pusheen decides to take her helicopter to her favourite restaurant. Due to various regulations, Pusheen is required to fix the helicopter at a specific altitude if she's not taking off or landing, so the helicopter can be effectively treated as traveling in a 2D plane. Due to fuel restrictions, Pusheen will travel only to points with nonnegative coordinates further subject to the constraint that and .

The helicopter is a bit finicky to maneuver, so in a single second, Pusheen can only change either the velocity of the helicopter in the x-direction or the y-direction by one unit. Pusheen is not required to change the velocity of the helicopter within a second. Note that the change in velocity happens before the helicopter moves within that second - this means that at the end of every second, Pusheen is guaranteed to be at a lattice point. Pusheen can only board/disembark from the helicopter when it is not moving.

More formally, if Pusheen's location at the beginning of second is and her helicopter's velocity is , and Pusheen decides to change the velocity at the start of that second by , then Pusheen's location at the beginning of second is and her helicopter's velocity is . Pusheen can only board/disembark when . Pusheen's helicopter will fly through all points on the line segment from to in that second.

It's also a windy day! Some lattice points have high winds that Pusheen must not fly through at any point in time. Help Pusheen figure out if she can get to her favourite restaurant, and figure out how quickly she can do so!

#### Constraints

Pusheen's starting and ending locations will not have wind.

All lattice points with wind are distinct.

In tests worth 1 mark, and .

In tests worth 2 additional marks, and .

In tests worth 3 additional marks, .

#### Input Specification

The first line contains three nonnegative integers, , , and .

The next line contains two nonnegative integers, and , representing Pusheen's starting location.

The next line contains two nonnegative integers, and , representing the location of Pusheen's favourite restaurant.

The next lines each contain two space-separated integers, and , representing a lattice point with wind.

#### Output Specification

If Pusheen cannot make it to her favourite restaurant, output -1. Otherwise, output the minimum number of seconds needed for Pusheen to be able to make it to her favourite restaurant.

#### Sample Input 1

1 0 0
0 0
1 0

#### Sample Output 1

2

#### Sample Input 2

4 0 0
0 0
4 0

#### Sample Output 2

4

#### Sample Input 3

1 1 0
0 0
1 1

#### Sample Output 3

4

#### Sample Input 4

2 0 1
0 0
2 0
1 0

#### Sample Output 4

-1