#P3645. [APIO2015] 雅加达的摩天楼
[APIO2015] 雅加达的摩天楼
Description
There are skyscrapers in Jakarta, the capital of Indonesia, arranged in a straight line and numbered from to from left to right. There are no other skyscrapers in Jakarta besides these .
There are mysterious creatures called "doge" living in Jakarta, numbered from to . Doge initially lives at skyscraper . Each doge has a mysterious power that allows it to jump between skyscrapers. The jumping ability of doge is ().
In one jump, a doge at skyscraper with jump ability can jump to skyscraper (if ) or to skyscraper (if ).
Doge is the leader of all doges, and it needs to deliver an urgent message to doge as soon as possible. Any doge that has received the message has two options:
- Jump to another skyscraper.
- Pass the message to another doge currently on the same skyscraper.
Please help the doges compute the minimum total number of jumps required to deliver the message from doge to doge , or determine that the message can never reach doge .
Input Format
The first line contains two integers and .
Each of the next lines contains two integers and .
Output Format
Output one line with the minimum number of steps required. If the message can never reach doge , output .
5 3
0 2
1 1
4 1
5
Hint
Sample explanation:
Below is a solution with a total of jumps:
- Doge jumps to skyscraper , then jumps to skyscraper ( jumps).
- Doge passes the message to doge .
- Doge jumps to skyscraper , then jumps to skyscraper , and then jumps to skyscraper ( jumps).
- Doge passes the message to doge .
Constraints:
All testdata guarantee .
Subtask 1 (10 points)
Subtask 2 (12 points)
Subtask 3 (14 points)
Subtask 4 (21 points)
Subtask 5 (43 points)
Translated by ChatGPT 5
京公网安备 11011102002149号