#P6399. [COI 2008] TAMNICA
[COI 2008] TAMNICA
Description

There is an infinite maze where the numbers are arranged as shown in the figure. A black line between two numbers means there is a wall separating them; if there is no line, then you can move between them.
You start from , and the exit is at . Now, because of an earthquake, some walls have collapsed, which makes it easier for you to move around. Taking advantage of the chaos, you need to escape as fast as possible by passing through as few numbers as you can. Please compute the minimum number of steps needed to escape.
Input Format
The first line contains an integer , which indicates the cell where the exit is located.
The second line contains an integer , which indicates the number of walls that collapsed due to the earthquake.
The next lines each contain an integer , meaning the number on one side of a collapsed wall.
The number on the other side is not given, but it is known that , so you can derive the value of yourself and thus determine which wall collapsed. Of course, some numbers (such as ) will never be a value of .
Output Format
Output one integer in one line, indicating the minimum number of numbers that need to be passed through (not including ).
31
9
15
25
30
6
9
19
24
27
4
6
10000
5
52
4
9
25
27
9953
Hint
Explanation for Sample 1

The state of the maze after the earthquake for Sample is shown in the figure.
The route escapes the fastest. A total of numbers are passed through.
Constraints
- For of the testdata, .
- For of the testdata, , , .
Note
Translated from COCI2007-2008 COI2008 T3 TAMNICA。
Translated by ChatGPT 5
京公网安备 11011102002149号