## WC '18 Contest 1 S3 - Reach for the Top

View as PDF

Points: 12 (partial)
Time limit: 1.8s
Memory limit: 128M

Author:
Problem type
##### Woburn Challenge 2018-19 Round 1 - Senior Division

It's time for Bob to face his biggest fear at H.S. High School: the dreaded gym class rope climb. In this brutal test of strength and endurance, Bob is tasked with ascending part of the way up an infinitely long vertical rope. He begins by grabbing onto the bottom of the rope, at a height of 0, and must reach any height of or greater (measured in metres).

To make matters even worse than usual, Alice has pranked Bob by spreading itching powder on some sections of the rope, which he'll need to avoid along the way! She's done so for sections, the -th of which covers all heights from to , inclusive . No two sections overlap with one another, even at their endpoints.

Bob's rope-climbing style is... unusual, to say the least, which may come in handy for avoiding Alice's itching powder. At any point, given that he's currently holding onto the rope at some height , he may only perform one of the following two possible actions:

1. Jump upwards by a height of exactly , such that his new height is , but only if the rope is not covered in itching powder at height .
2. Drop downwards by any height, such that his new height is any integer , but only if the rope is not covered in itching powder at height .

Both types of actions involved in this technique are understandably quite tiring, so Bob would like to avoid performing more of them than necessary. Help him determine the minimum number of actions he must perform to reach a height of at least metres, or determine that it's sadly impossible for him to ever reach such a height.

In test cases worth 8/27 of the points, and .
In test cases worth another 12/27 of the points, .

#### Input Specification

The first line of input consists of three space-separated integers, , , and .
lines follow, the -th of which consists of two space-separated integers, and , for .

#### Output Specification

Output a single integer, either the minimum number of actions required for Bob to reach a height of at least metres, or -1 if it's impossible for him to do so.

#### Sample Input 1

12 5 2
2 4
10 10

#### Sample Output 1

5

#### Sample Input 2

5 2 2
1 1
4 4

#### Sample Output 2

-1

#### Sample Explanation

In the first case, Bob can jump up to a height of , drop down to a height of , jump up to a height of , jump up to a height of , and finally jump up to a height of . This is the only strategy which involves actions, which is the minimum possible number of actions.

In the second case, Bob must start by jumping up to a height of . From there, he may not jump upwards to a height of (as the rope is covered in itching powder there), and similarly may not drop down to a height of , meaning that he can never grab the rope at any height aside from or .