Jakarta Skyscrapers
Time limit1sMemory limit256 MB
Doge 0 spreads news by jumping between buildings with its own stride or handing it to a doge on the same building, and you count the fewest jumps to doge 1.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, BFS
- Solved
- No attempts yet
Problem
Jakarta has tall buildings standing in a straight line. They are numbered from the left. The city has no other tall buildings.
mysterious creatures called doges live in the city. The doges are numbered . Doge starts on building , and the strength of its mysterious power is the positive integer . A doge with power standing on building can move to building or building in one jump. The destination number must be at least and less than .
Doge 0 leads all the doges. It has urgent news for doge 1 and wants the news delivered as fast as possible. A doge that has heard the news can do one of two things.
- Jump to another building with its own power.
- Pass the news to another doge standing on the same building.
Write a program that computes the smallest number of jumps needed to bring the news to doge 1. If there is no way to deliver it, detect that as well.
Input
The first line contains the integers and . Each of the next lines contains two integers and , one doge per line.
Output
Print the smallest number of jumps on the first line. Print if the news cannot reach doge 1.
Hint
Consider a city with 5 buildings and 3 doges. Doge 0 is on building 0 with power 2, doge 1 is on building 1 with power 1, and doge 2 is on building 4 with power 1. The following order delivers the news in 5 jumps.
- Doge 0 jumps to building 2, then jumps to building 4. (2 jumps)
- Doge 0 passes the news to doge 2 on building 4.
- Doge 2 jumps to building 3, then building 2, then building 1. (3 jumps)
- Doge 2 passes the news to doge 1 on building 1.