Skip to main content
Back to problems
Codeforces
Easy
Arrays
Simulation
Shell Game

Track the position of a hidden ball as three shells are repeatedly swapped.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

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 nn — the number of swaps.
  • The next nn lines each contain two integers aa and bb indicating that the shells in positions aa and bb 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

  • 1n1001 \le n \le 100
  • Each swap uses distinct shell labels from the set {1, 2, 3}
  • All swaps are valid
  • Time limit friendly solution: O(n)O(n)
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.