The Impostor
Time limit1sMemory limit1024 MB
Simulate the rounds to find when too many crew members know the impostor's identity, or report N if he wins.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
Martynas and his N friends play the video game The Impostor. The action takes place on a spaceship made up of M rooms. At the start of the game every player is secretly given a role: exactly one player is the impostor, and all the others are crew members.
The crew's goal is to identify the impostor while keeping up with the ship's tasks; the impostor's goal is to remain the only player left on the ship.
The Impostor is played in rounds. During a round:
- Every surviving player goes to the room assigned to them for that round.
- The crew members carry out their maintenance tasks.
- The impostor picks a victim who is in the same room as him and removes that player from the ship. The impostor is always assigned to a room in which he is not alone.
- Everyone who is in the same room as the impostor sees him remove a player. They therefore learn who the impostor is and remember it for the rest of the game.
- After the round, every player still on the ship leaves their room and votes by pressing either the red or the yellow button. Players who know who the impostor is press the red button; players who do not press the yellow button. Martynas, the impostor, also votes, and always presses the yellow button so as not to give himself away.
- If more red buttons than yellow buttons are pressed, the impostor is unmasked: he loses and the game stops.
The impostor wins if all N crew members are removed and he (the (N+1)-th player) is the only one left in the game.
Martynas has found out that in an upcoming game he will be the impostor, and he also knows which player will be sent to which room in every round. He analysed this information and planned in advance which player he will remove in each round: in round i he removes player . Because the impostor can only remove someone from his own room, in round i the impostor is in the room that player is assigned to that round.
Determine whether Martynas will win the game, and if not, in which round he will be unmasked.

Input
The first line contains two positive integers: , the number of players who are not the impostor, and , the number of rooms.
The second line contains distinct positive integers — the numbers of the players Martynas removes in round i.
The next lines each contain positive integers. In the i-th of these lines, the j-th number is — the room player j goes to in round i, assuming that player has not been removed before that round.
Output
Print a single positive integer: if Martynas wins the game, otherwise the number of the round in which Martynas is removed from the game.
Constraints
- , and all are distinct.