Game of Death
InterviewTime limit2sMemory limit256 MB
Given a fixed pointing map for N people, find the smallest positive M so that starting at person 0 and following M pointers ends on person K.
- Level
Medium5 of 10
- Topics
- Graph, Simulation, Brute force, Implementation
- Solved
- No attempts yet
Problem
Kim Young-gi, a 17th-year student who has just met the freshmen of Chung-Ang University's College of Software, is trying to get close to them despite the two-year gap. One of his efforts is joining them for a drinking party. Too embarrassed to go alone, Young-gi persuaded his classmate Bo-seong to come along. When the mood starts to sink, Young-gi and Bo-seong propose a drinking game called The Game of Death.
The rules of the Game of Death are simple.
N people in the game sit around a round table. The person who starts the game is number 0, the person to their right is number 1, the next to the right is number 2, and the person to the right of number N-1 is number 0 again.
As soon as number 0 shouts "Exciting! Awesome, this is fun! Awesome, The Game of De! ath!", everyone points at one of the people seated around the table. Then number 0 calls out an arbitrary positive integer M.
Next, number 0 says "1". The person pointed at by the one who said "1" says "2", the person pointed at by the one who said "2" says "3", and this repeats the same way up to M. The person pointed at by the one who last says M drinks a penalty shot.
Feeling guilty about making a freshman drink, Young-gi decides to attack his classmate Bo-seong instead. Given the completed pointing among the game's participants, whisper to Bo-seong the smallest positive integer M that Young-gi must call out for Bo-seong to drink the penalty shot.
Kim Young-gi is naturally number 0, since he proposed the game.
Input
The first line gives the number of people in the game N (3 ≤ N ≤ 150) and Bo-seong's number K (1 ≤ K ≤ N - 1), separated by a space.
From the second line, over N lines, the number ai (0 ≤ ai ≤ N - 1) that person i (0 ≤ i ≤ N - 1) points at is given. A person may point at themselves.
Output
Print the smallest positive integer M that Young-gi must say. If Bo-seong cannot be caught by any means, print -1.