Track the position of a hidden ball as three shells are repeatedly swapped.
You are given a game with three shells labeled 1, 2, and 3. A ball starts under shell 1. Then a sequence of swaps is performed, where each swap exchanges the positions of two shells. Your task is to determine which shell contains the ball after all swaps are complete.
This is a simple simulation problem: follow every swap in order and keep track of the ball’s current position.
Input Format
The input consists of:
- The first line contains an integer — the number of swaps.
- The next lines each contain two integers and indicating that the shells in positions and are swapped.
The ball initially starts under shell $1$.
Output Format
Print a single integer: the final position of the ball after all swaps are processed.
Constraints
- Each swap uses distinct shell labels from the set {1, 2, 3}
- All swaps are valid
- Time limit friendly solution:
Example 1
Input
3 1 2 3 2 1 3
Output
2
Explanation
Start with the ball under shell 1.
- Swap 1 and 2: ball moves to shell 2.
- Swap 3 and 2: ball moves to shell 3.
- Swap 1 and 3: ball moves to shell 1.
Final answer: 1
Example 2
Input
2 2 3 1 2
Output
3
Explanation
Start at shell 1.
- Swap 2 and 3: ball stays at shell 1.
- Swap 1 and 2: ball moves from 1 to 2.
Final answer: 2
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.