LogiQuest

351. Snakes and Ladders

HardBFS

On a 1..100 board, a die roll moves you 1–6 squares; snakes and ladders move you from their start to their end. Print the minimum number of rolls to reach 100 from 1, or -1.

Input format

Line 1: k (number of snakes + ladders) Next k lines: from to

Output format

The minimum rolls, or -1.

Example 1

Input

2
32 62
42 68

Output

13

Example 2

Input

0

Output

17

Constraints

0 ≤ k ≤ 50

Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.

Loading the MyLoginn IDE…