Brave Sir Robin has been thrown in the dungeon by the evil king. The dungeon consists of an infinite number of cube-shaped rooms with big stone walls. Rooms are connected by passages so that the entire dungeon, when viewed from above, looks like a spiral. The rooms are numbered as follows:
After a big earthquake some of the walls collapsed, and new passages were formed between adjacent rooms.
Sir Robin is initially in room . Sir Robin knows that the exit from the dungeon is located in room , and wants to escape while everyone is distracted by the earthquake. Because the evil dragon is guarding the dungeon, Sir Robin wants to use the fastest way out of the dungeon.
Write a program that, given the location of the exit and the list of new passages, determines the smallest number of passages that Sir Robin must go through before he can exit the dungeon.
Input Specification
The first line of input contains an integer , the room in which the exit is located.
The second line of input contains an integer , the number of new passages.
Each of the following lines contains one integer , meaning that a new passage now connects adjacent rooms and , where . The number is not given explicitly, but it can be uniquely determined from (for example, if is , then must be ). Also, some rooms can never be room (rooms etc.).
Output Specification
Output should consist of a single integer, the smallest number of passages that Sir Robin must go through before he can exit the dungeon.
Scoring
In a number of test cases, worth a total of points, will be at most .
Sample Input 1
31
9
15
25
30
6
9
19
24
27
4
Sample Output 1
6
Explanation for Sample Output 1
This is the layout of the dungeon after the earthquake:
Mirko can use the route , using only hallways to exit the dungeon.
Sample Input 2
10000
5
52
4
9
25
27
Sample Output 2
9953
Comments