We need to determine initial position given n moves and final position x (0,1,2). Moves alternate: odd moves (1st, 3rd, 5th, ...) swap shells 0 and 1; even moves (2nd, 4th, 6th, ...) swap shells 1 and 2.
We can simulate, but n up to 2e9, so we need pattern.
Let's denote state as position of ball (0,1,2). Moves are deterministic swaps based on parity.
We can think of the permutation on {0,1,2} for each move. Odd move: permutation A = (0 1) transposition, i.e., swaps 0 and 1, leaves 2 fixed. Even move: permutation B = (1 2) transposition, swaps 1 and 2, leaves 0 fixed.
We have sequence of n moves: if n is odd, the first move is odd, then even, etc.
We need to apply composite permutation P_n = (move n) ∘ (move n-1) ∘ ... ∘ (move 1) to initial position to get final x. So initial = P_n^{-1}(x). Since permutations are small, we can compute cycle or find pattern.
Compute effect after 1 move:
- If move1: A: mapping: 0→1, 1→0, 2→2. So if initial i, after 1 move: i maps to? Actually we apply move1 (swap 0,1) so new position = i if i=2, else flips 0<->1. So permutation A.
After 2 moves: first A then B. Compose: B∘A. Let's compute: A then B means apply A first then B? Usually we think of moves in order: move1 then move2. So final = move2( move1(initial) ). So overall permutation = B ∘ A. Compute mapping:
- Start 0: after A→1, then B (swap1,2) →2. So 0→2.
- 1: after A→0, then B (swap1,2; 0 unchanged) →0. So 1→0.
- 2: after A→2, then B: 2→1 (since swap1,2). So 2→1.
So B∘A yields permutation: 0→2, 1→0, 2→1. That's a 3-cycle: (0 2 1) (i.e., 0→2, 2→1, 1→0). In cycle notation (0 2 1). It's a cyclic shift? Actually if we list positions [0,1,2] after two moves, mapping: 0->2,1->0,2->1. That's like rotation left? 0 goes to 2; 1 goes to 0; 2 goes to 1. That's a rotation right? Let's not worry.
After 3 moves: sequence: A (odd1), B (even2), A (odd3). Overall = A ∘ B ∘ A? Actually careful: order: move1 then move2 then move3. So final = move3(move2(move1(initial))) = A(B(A(initial)))? Since move3 is A. So overall = A ∘ B ∘ A. Compute composition: (A∘B)∘A? but easier compute stepwise: After 2 moves we have mapping P2 = B∘A. Then apply A: overall = A ∘ (B∘A) = A∘B∘A. Compute mapping: Use P2 mapping above: 0→2,1→0,2→1. Then apply A: swaps 0<->1, leaves 2 fixed. So:
- 0: P2(0)=2, then A: 2 unchanged => 2. So overall 0→2.
- 1: P2(1)=0, then A: 0→1? Wait A maps 0 to 1, 1 to 0, 2 to 2. So after P2=0, applying A yields 1. So 1→1.
- 2: P2(2)=1, then A: 1→0 => 0. So 2→0.
Thus A∘B∘A yields: 0→2, 1→1, 2→0. That's like swapping 0 and 2? Indeed it's transposition (0 2) with 1 fixed. Let's confirm: 0→2, 2→0, 1→1. So yes.
After 4 moves: moves: A, B, A, B. Overall = B ∘ A ∘ B ∘ A? Actually order: move1 A, then B, then A, then B. So final = B(A(B(A(initial)))) = B ∘ A ∘ B ∘ A. That's (B∘A)∘(B∘A)? Not exactly because composition order: (B∘(A∘(B∘A)))? But since composition is associative, it's B∘A∘B∘A. Compute by combining P2 = B∘A, then next two moves: another B∘A? Actually after first two moves we have P2 = B∘A. After next two moves (move3 A, move4 B), we apply A then B? Wait: after first two, state = P2(initial). Then move3 (A) gives A(P2(initial)). Then move4 (B) gives B(A(P2(initial))) = (B∘A∘(B∘A))(initial) = (B∘A)∘(B∘A) (since composition is associative, B∘A∘B∘A = (B∘A) ∘ (B∘A)? Actually (B∘A)∘(B∘A) = B∘A∘B∘A, yes because (f∘g)∘(h∘k) = f∘g∘h∘k but careful with order: (B∘A)∘(B∘A) means first apply the rightmost (B∘A) then leftmost (B∘A). So it's B∘A∘(B∘A) = B∘A∘B∘A. So indeed overall = (B∘A)^2. So P4 = (B∘A)^2.
We computed B∘A gives 3-cycle (0→2,1→0,2→1). So let's denote C = B∘A. Then C^2 = C∘C. Compute: C mapping: 0→2, 1→0, 2→1. So C^2: 0→C(C(0)) = C(2) = 1; 1→C(C(1)) = C(0)=2; 2→C(C(2)) = C(1)=0. So C^2: 0→1,1→2,2→0. That's the other direction cycle (0 1 2) maybe. C^3: should be identity because 3-cycle. Indeed C^3 = identity. So P4 = C^2.
Thus P4 is (0→1,1→2,2→0). So after 4 moves, mapping is a cycle.
Now, P5 = after 5 moves: moves: A,B,A,B,A. That's A after P4: P5 = A ∘ P4 = A ∘ C^2. Compute: P4 mapping: 0→1,1→2,2→0. Then apply A: swaps 0<->1, leaves 2 fixed.
- 0: P4(0)=1, then A: 1→0 => 0
- 1: P4(1)=2, then A: 2→2 => 2
- 2: P4(2)=0, then A: 0→1 => 1
So P5: 0→0,1→2,2→1. That's swapping 1 and 2? Actually 1↔2, 0 fixed: transposition (1 2). Indeed.
P6 = after 6 moves: moves: A,B,A,B,A,B = B∘P5? Actually after 5 moves we have P5, then move6 is B. So P6 = B ∘ P5 = B ∘ A ∘ C^2? But also note that 6 moves is 3 pairs: (B∘A) repeated 3 times = C^3 = identity. Let's compute: P6 = (B∘A)^3 = C^3 = identity. Indeed from above: 6 moves yields identity mapping. Check: P6 = B∘A∘B∘A∘B∘A? Actually order: moves: A,B,A,B,A,B => overall = B ∘ A ∘ B ∘ A ∘ B ∘ A? Wait careful: moves: 1:A,2:B,3:A,4:B,5:A,6:B => final = B(A(B(A(B(A(initial)))))) = B∘A∘B∘A∘B∘A. That's (B∘A) repeated 3 times but note that composition order: (B∘A) repeated thrice yields C^3? Actually C = B∘A, so C^2 = B∘A∘B∘A? But careful: C∘C = (B∘A)∘(B∘A) = B∘A∘B∘A (since composition associativity). So C^3 = C ∘ C ∘ C = (B∘A)∘(B∘A)∘(B∘A) = B∘A∘B∘A∘B∘A. Yes. So P6 = C^3 = identity. So after 6 moves, ball returns to original position.
Thus the overall permutation repeats every 6 moves. Since n up to 2e9, we can reduce n modulo 6 to compute the net permutation for n moves.
We need to find initial position i such that after n moves we get x. Equivalent to: given x, we want i = inverse mapping of P_n applied to x. Since P_n is a permutation (bijective), we can compute the mapping for n%6 and then find i. So we can compute for each residue r = n mod 6 (where r in 0..5) the permutation P_r (with r moves; careful: if r=0 means n multiple of 6, which is identity). Then initial = P_n^{-1}(x). But easier: we can simulate ball forward from each possible starting position 0,1,2 for r moves and see which yields x. Since r is small (0..5), that's trivial.
But we need to be careful about mapping: n moves: we apply moves 1..n. For r = n mod 6, we can compute the net effect of r moves (if n >= 6, the full cycles of 6 are identity, so net effect is same as r moves). So we just need to compute for residue r.
Thus we can compute: for each i in {0,1,2}, simulate r moves (with parity: odd moves swap 0-1, even swaps 1-2) and see final = x? But careful: If r = 0, then no moves, so initial = x directly. So answer = x.
If r > 0, we simulate from i? Actually we need i such that after r moves we get x. So we can either compute forward from i and check, or compute inverse mapping. Simpler: compute forward from all three i for r moves and see which matches x. Since r <=5, that's fine.
But we need to be careful about the order: moves 1..r. The first move is always odd (swap 0-1) because move numbers start at 1. So for any r, the sequence of swaps is predetermined: move 1: swap0-1; move2: swap1-2; move3: swap0-1; move4: swap1-2; etc. So for given r, we can simulate.
Thus algorithm:
- read n, x.
- r = n % 6.
- for candidate in [0,1,2]:
pos = candidate
for move in range(1, r+1):
if move % 2 == 1: # odd
if pos == 0: pos = 1
elif pos == 1: pos = 0
# else pos unchanged (2)
else: # even
if pos == 1: pos = 2
elif pos == 2: pos = 1
# else unchanged (0)
if pos == x: print(candidate) and break
That should work.
We need to confirm examples:
Example1: n=4, x=2. r=4%6=4. Simulate from each:
Candidate 0:
move1(odd): swap0-1 => pos becomes 1? Since pos=0->1
move2(even): swap1-2 => pos=1 becomes 2 => pos=2
move3(odd): swap0-1 => pos=2 unchanged? Actually only swaps0-1 so pos stays2
move4(even): swap1-2 => pos=2 becomes1? Actually swap1-2: if pos==2 ->1, if pos==1->2, else unchanged. So pos=2->1. final=1, not 2.
Candidate1:
start1.
move1(odd): 1->0
move2(even): 0 unchanged? (swap1-2 leaves0) ->0
move3(odd): 0->1
move4(even): 1->2 -> final2. matches. So output1. Good.
Candidate2: start2.
move1: odd: 2 unchanged ->2
move2: even: 2->1
move3: odd: 1->0
move4: even: 0 unchanged ->0. not 2.
So answer 1. Works.
Example2: n=1, x=1. r=1. Simulate:
candidate0: move1 odd: 0->1 => final1 -> matches. So output0? Wait answer example says 0. So yes candidate0 works. Actually also check candidate1: start1->0, not match; candidate2:2->2 not match. So output0 correct.
Thus algorithm correct.
We need to consider n very large but modulo reduces.
Edge case: n=0? But constraint says n≥1. So fine.
Thus solution.
We can also compute mapping explicitly. Let's derive formulas.
But given simplicity, we can just simulate.
Time O(1).
Now writing solution in Python:
def solve():
n = int(input())
x = int(input())
r = n % 6
for start in range(3):
pos = start
for move in range(1, r+1):
if move % 2 == 1:
# swap 0 and 1
if pos == 0:
pos = 1
elif pos == 1:
pos = 0
# else pos unchanged
else:
# swap 1 and 2
if pos == 1:
pos = 2
elif pos == 2:
pos = 1
# else unchanged
if pos == x:
print(start)
return
Works.
But there is nuance: If r=0, loop for move in range(1,0+1) is empty, so pos = start; we compare to x and output start that matches. Since start unique? Actually x is given, only one start will match because it's a permutation. So fine.
Thus solution.
We can also compress logic:
def solve():
n = int(input())
x = int(input())
mod = n % 6
swaps = [(0,1), (1,2)] * ( (mod+1)//2 )? Not needed.
All good.
Now in the problem statement, n up to 2e9, but we use modulo.
Now let's ensure we read input correctly: two lines, each containing integer.
Thus final answer.
We can also derive mathematical mapping: after n moves, mapping can be represented as:
Let n mod 6 = r. For each r, we can create a direct mapping from final to initial.
But simulation is fine.
Thus answer.
Now produce final solution with explanation. ```python
def solve() -> None:
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
x = int(data[1])
r = n % 6 # only the remainder matters, pattern repeats every 6 moves
# try each possible starting position 0,1,2
for start in range(3):
pos = start
for move in range(1, r + 1):
if move % 2 == 1: # odd move: swap left (0) and middle (1)
if pos == 0:
pos = 1
elif pos == 1:
pos = 0
# else (pos == 2) stays unchanged
else: # even move: swap middle (1) and right (2)
if pos == 1:
pos = 2
elif pos == 2:
pos = 1
# else (pos == 0) stays unchanged
if pos == x:
print(start)
return
if name == "main":
solve()