Attendance Check
InterviewTime limit1sMemory limit1024 MB
Given a permutation forming one cycle, find the student who responds last when the calling chain starts at student k.
- Level
Easy3 of 10
- Topics
- Implementation, Simulation, Array, Graph
- Solved
- No attempts yet
Problem
Teacher Neringa has devised a new way to check whether all students have gathered for class. There are students in the class, and each student is assigned to remember the name of exactly one other student.
Each day before class, Neringa randomly picks one student and calls that student's name. The student who is called then calls the name of the student assigned to them; that student in turn calls the name of the student assigned to them; and so on, until some student calls the name of the student Neringa originally called.
Neringa set up the assignments (deciding who remembers whose name) so that, when every student is present, calling the students this way is guaranteed to call each student exactly once. In other words, the assignments form a single cycle that includes all students.
Determine which student responds last, assuming all students are present.
Input
The first line contains two positive integers: the number of students and the number of the student Neringa calls.
The second line contains positive integers , where is the number of the student assigned to student .
Output
Print a single integer — the number of the last student to respond.
Constraints
- for every .
- All are distinct.