Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is "Bob", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, "Alice". 

So now Ilya wants to compare his robots' performance in a simple game called "1-2-3". This game is similar to the "Rock-Paper-Scissors" game: both robots secretly choose a number from the set {1, 2, 3} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3. 

Both robots' programs make them choose their numbers in such a way that their choice in (i + 1)-th game depends only on the numbers chosen by them in i-th game. 

Ilya knows that the robots will play k games, Alice will choose number a in the first game, and Bob will choose b in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all k games, so he asks you to predict the number of points they will have after the final game. 


-----Input-----

The first line contains three numbers k, a, b (1 ≤ k ≤ 10^18, 1 ≤ a, b ≤ 3). 

Then 3 lines follow, i-th of them containing 3 numbers A_{i}, 1, A_{i}, 2, A_{i}, 3, where A_{i}, j represents Alice's choice in the game if Alice chose i in previous game and Bob chose j (1 ≤ A_{i}, j ≤ 3). 

Then 3 lines follow, i-th of them containing 3 numbers B_{i}, 1, B_{i}, 2, B_{i}, 3, where B_{i}, j represents Bob's choice in the game if Alice chose i in previous game and Bob chose j (1 ≤ B_{i}, j ≤ 3). 


-----Output-----

Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after k games.


-----Examples-----
Input
10 2 1
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2

Output
1 9

Input
8 1 1
2 2 1
3 3 1
3 1 3
1 1 1
2 1 1
1 2 3

Output
5 2

Input
5 1 1
1 2 2
2 2 2
2 2 2
1 2 2
2 2 2
2 2 2

Output
0 0



-----Note-----

In the second example game goes like this:

$(1,1) \rightarrow(2,1) \rightarrow(3,2) \rightarrow(1,2) \rightarrow(2,1) \rightarrow(3,2) \rightarrow(1,2) \rightarrow(2,1)$

The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice.

Difficulty: MEDIUM_HARD
Tags: Graph algorithms

Implementation

Starter code:

Solutions:
import sys
(k, a, b) = map(int, input().split())
next_a = [[0] * 4] + [[0] + list(map(int, input().split())) for _ in range(3)]
next_b = [[0] * 4] + [[0] + list(map(int, input().split())) for _ in range(3)]
dp = [[-1] * 4 for _ in range(4)]
dp_score = [[[0, 0] for _ in range(4)] for _ in range(4)]
score = [0, 0]
i = k
while i:
	if i < k and dp[a][b] != -1:
		m = i // (dp[a][b] - i)
		score[0] += (score[0] - dp_score[a][b][0]) * m
		score[1] += (score[1] - dp_score[a][b][1]) * m
		i %= dp[a][b] - i
	if i:
		dp[a][b] = i
		dp_score[a][b] = score[:]
		if a == 3 and b == 2 or (a == 2 and b == 1) or (a == 1 and b == 3):
			score[0] += 1
		elif b == 3 and a == 2 or (b == 2 and a == 1) or (b == 1 and a == 3):
			score[1] += 1
		(a, b) = (next_a[a][b], next_b[a][b])
		i -= 1
print(*score)


R = lambda : list(map(int, input().split()))
(k, a, b) = R()
a -= 1
b -= 1
A = [list(R()) for _ in range(3)]
B = [list(R()) for _ in range(3)]
f = [[None] * 3 for _ in range(3)]
s = [[0, 0]]
i = 0
while i < k and f[a][b] is None:
	f[a][b] = i
	s.append(list(s[i]))
	i += 1
	d = a - b
	if d < 0:
		d += 3
	if d:
		s[i][d - 1] += 1
	(a, b) = (A[a][b] - 1, B[a][b] - 1)
if i == k:
	print(*s[i])
else:
	cb = f[a][b]
	c = i - cb
	m = (k - i) // c
	(sa, sb) = s[i]
	sa += m * (sa - s[cb][0]) + s[cb + (k - i) % c][0] - s[cb][0]
	sb += m * (sb - s[cb][1]) + s[cb + (k - i) % c][1] - s[cb][1]
	print(sa, sb)


completed = {}
alice = [[0, -1, 1], [1, 0, -1], [-1, 1, 0]]
bob = [[0, 1, -1], [-1, 0, 1], [1, -1, 0]]

def simulate(i):
	nonlocal s, a, b, l, m, completed
	if str(a) + ' ' + str(b) in completed:
		return
	s.append(alice[a - 1][b - 1])
	temp1 = a
	temp2 = b
	completed[str(a) + ' ' + str(b)] = i
	a = l[temp1 - 1][temp2 - 1]
	b = m[temp1 - 1][temp2 - 1]
	simulate(i + 1)
(k, a, b) = list(map(int, input().split()))
l = []
for i in range(3):
	l.append(list(map(int, input().split())))
m = []
for i in range(3):
	m.append(list(map(int, input().split())))
s = []
ali = 0
bo = 0
simulate(0)
for i in range(completed[str(a) + ' ' + str(b)]):
	if k == 0:
		break
	k -= 1
	if s[i] == 1:
		ali += 1
	elif s[i] == -1:
		bo += 1
s = s[completed[str(a) + ' ' + str(b)]:]
awins = s.count(1)
bwins = s.count(-1)
mul = k // len(s)
rem = k % len(s)
ali += mul * awins
bo += mul * bwins
for i in range(rem):
	if s[i] == 1:
		ali += 1
	elif s[i] == -1:
		bo += 1
print(ali, bo)


R = lambda : list(map(int, input().split()))
(k, a, b) = R()
a -= 1
b -= 1
A = [list(R()) for _ in range(3)]
B = [list(R()) for _ in range(3)]
f = [[None] * 3 for _ in range(3)]
s = [[0, 0]]
i = 0
while i < k and f[a][b] is None:
	f[a][b] = i
	s.append(list(s[i]))
	i += 1
	d = a - b
	if d < 0:
		d += 3
	if d:
		s[i][d - 1] += 1
	(a, b) = (A[a][b] - 1, B[a][b] - 1)
if i == k:
	print(*s[i])
else:
	j = f[a][b]
	(m, n) = divmod(k - i, i - j)
	print(*[(m + 1) * (x[0] - x[1]) + x[2] for x in zip(s[i], s[j], s[j + n])])


class Game123:

	def __init__(self):
		self.alice = [0] * 9
		self.bob = [0] * 9
		self.moves = [-1] * 10
		self.count = 0
		self.pointsA = 0
		self.pointsB = 0

	def gameChoice(self, m, r, bBob):
		choice = self.alice
		if bBob:
			choice = self.bob
		for i in range(len(m)):
			choice[3 * r + i] = m[i]

	def findMove(self, a, b):
		m = (a - 1) * 3 + b - 1
		for i in range(self.count):
			if self.moves[i] == m:
				return i
		self.moves[self.count] = m
		self.count += 1
		return -1

	def computePoints(self, m):
		b = m % 3 + 1
		a = m // 3 + 1
		if a == b:
			return
		if a == 3 and b == 2 or (a == 2 and b == 1) or (a == 1 and b == 3):
			self.pointsA += 1
		else:
			self.pointsB += 1

	def output(self):
		print('{0} {1}'.format(self.pointsA, self.pointsB))

	def startGame(self, a, b, totalmoves):
		found = -1
		while found < 0:
			found = self.findMove(a, b)
			m = (a - 1) * 3 + b - 1
			a = self.alice[m]
			b = self.bob[m]
		pointaA = 0
		pointsB = 0
		if totalmoves <= self.count:
			for i in range(totalmoves):
				self.computePoints(self.moves[i])
		else:
			totalmoves -= found
			repeats = totalmoves // (self.count - found)
			totalmoves %= self.count - found
			for i in range(found, self.count):
				self.computePoints(self.moves[i])
			self.pointsA *= repeats
			self.pointsB *= repeats
			for i in range(found):
				self.computePoints(self.moves[i])
			for i in range(found, totalmoves + found):
				self.computePoints(self.moves[i])
		self.output()

	def test():
		g = Game123()
		g.gameChoice([2, 2, 1], 0, False)
		g.gameChoice([3, 3, 1], 1, False)
		g.gameChoice([3, 1, 3], 2, False)
		g.gameChoice([1, 1, 1], 0, True)
		g.gameChoice([2, 1, 1], 1, True)
		g.gameChoice([1, 2, 3], 2, True)
		g.startGame(1, 1, 8)

def nia():
	s = input()
	while len(s) == 0:
		s = input()
	s = s.split()
	iVal = []
	for i in range(len(s)):
		iVal.append(int(s[i]))
	return iVal

def solve():
	g = Game123()
	nab = nia()
	for i in range(3):
		g.gameChoice(nia(), i, False)
	for i in range(3):
		g.gameChoice(nia(), i, True)
	g.startGame(nab[1], nab[2], nab[0])
solve()


(k, a, b) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(3)]
B = [list(map(int, input().split())) for _ in range(3)]
base = 4
d = {}

def score(x, y):
	r = (x - y + 3) % 3
	if r == 0 or r == 2:
		return 0
	if r == 1:
		return 1
route_a = [score(a, b)]
route_b = [score(b, a)]
d[a + b * 4] = len(d)
index = None
while True:
	a_ = A[a - 1][b - 1]
	b_ = B[a - 1][b - 1]
	a = a_
	b = b_
	cur = a + 4 * b
	if cur in d:
		index = d[cur]
		break
	d[cur] = len(d)
	route_a.append(score(a, b))
	route_b.append(score(b, a))
score_a = 0
score_b = 0
N = len(route_a)
f0 = index
f = N - index
if k <= f0:
	score_a = sum(route_a[:k])
	score_b = sum(route_b[:k])
else:
	x = (k - f0) // f
	r = (k - f0) % f
	score_a = sum(route_a[:f0]) + x * sum(route_a[f0:]) + sum(route_a[f0:f0 + r])
	score_b = sum(route_b[:f0]) + x * sum(route_b[f0:]) + sum(route_b[f0:f0 + r])
print(score_a, score_b)


(k, a, b) = [int(x) for x in input().split()]
ali = []
bob = []
for i in range(3):
	ali.append([int(x) for x in input().split()])
for i in range(3):
	bob.append([int(x) for x in input().split()])
res = [(a, b)]
sc = []
if a == 1 and b == 3:
	sc.append((1, 0))
elif a == 3 and b == 1:
	sc.append((0, 1))
elif a > b:
	sc.append((1, 0))
elif b > a:
	sc.append((0, 1))
else:
	sc.append((0, 0))
ci = -1
while True:
	if len(res) == k:
		break
	na = ali[a - 1][b - 1]
	nb = bob[a - 1][b - 1]
	(a, b) = (na, nb)
	if (a, b) in res:
		ci = res.index((a, b))
		break
	res.append((a, b))
	if a == 1 and b == 3:
		sc.append((1, 0))
	elif a == 3 and b == 1:
		sc.append((0, 1))
	elif a > b:
		sc.append((1, 0))
	elif b > a:
		sc.append((0, 1))
	else:
		sc.append((0, 0))
(tsa, tsb) = (0, 0)
for i in range(ci):
	tsa += sc[i][0]
	tsb += sc[i][1]
res = res[ci:]
if ci == -1:
	for i in range(k):
		tsa += sc[i][0]
		tsb += sc[i][1]
else:
	k -= ci
	(cscali, cscbob) = (0, 0)
	for i in range(ci, len(sc)):
		cscali += sc[i][0]
		cscbob += sc[i][1]
	csc = (cscali, cscbob)
	fc = k // len(res)
	tsa += csc[0] * fc
	tsb += csc[1] * fc
	pc = k % len(res)
	for i in range(ci, ci + pc):
		tsa += sc[i][0]
		tsb += sc[i][1]
print(tsa, tsb)


(ka, kb) = (0, 0)

def g(a, b):
	global ka, kb
	if a == b:
		return
	if abs(a - b) == 2:
		if a == 1:
			ka += 1
		else:
			kb += 1
	elif a > b:
		ka += 1
	else:
		kb += 1

def main():
	global ka, kb
	(k, a, b) = map(int, input().split())
	aa = [[] for i in [1, 1, 1]]
	bb = [[] for i in [1, 1, 1]]
	aa[0] = list(map(int, input().split()))
	aa[1] = list(map(int, input().split()))
	aa[2] = list(map(int, input().split()))
	bb[0] = list(map(int, input().split()))
	bb[1] = list(map(int, input().split()))
	bb[2] = list(map(int, input().split()))
	x = a
	y = b
	o = 1
	c = [[x, y]]
	d = [[0, 0]]
	g(x, y)
	if k == 1:
		return
	(x, y) = (aa[x - 1][y - 1], bb[x - 1][y - 1])
	while [x, y] not in c:
		o += 1
		c += [[x, y]]
		d += [[ka, kb]]
		g(x, y)
		(x, y) = (aa[x - 1][y - 1], bb[x - 1][y - 1])
		if o == k:
			return
	ind = c.index([x, y])
	dka = ka - d[ind][0]
	dkb = kb - d[ind][1]
	delta = o - ind
	kk = (k - ind) // delta - 1
	ka += kk * dka
	kb += kk * dkb
	o += kk * delta
	while o != k:
		o += 1
		g(x, y)
		(x, y) = (aa[x - 1][y - 1], bb[x - 1][y - 1])
main()
print(ka, kb)


def solve():
	point123 = {(1, 1): None, (1, 2): 'Bob', (1, 3): 'Alice', (2, 1): 'Alice', (2, 2): None, (2, 3): 'Bob', (3, 1): 'Bob', (3, 2): 'Alice', (3, 3): None}
	map123 = {}
	cycle123 = {}
	(k, a, b) = [int(st) for st in input().split(' ')]
	A = [None, None, None, None]
	B = [None, None, None, None]
	for i in range(1, 4):
		A[i] = [None] + [int(st) for st in input().split(' ')]
	for i in range(1, 4):
		B[i] = [None] + [int(st) for st in input().split(' ')]
	for i in range(1, 4):
		for j in range(1, 4):
			map123[i, j] = (A[i][j], B[i][j])
	for i in range(1, 4):
		for j in range(1, 4):
			cycle = 1
			(again, bgain) = (int(point123[i, j] == 'Alice'), int(point123[i, j] == 'Bob'))
			(i1, j1) = map123[i, j]
			while cycle < 10 and (not (i1 == i and j1 == j)):
				cycle += 1
				again += int(point123[i1, j1] == 'Alice')
				bgain += int(point123[i1, j1] == 'Bob')
				(i1, j1) = map123[i1, j1]
			if cycle < 10:
				cycle123[i, j] = (cycle, again, bgain)
	(ansA, ansB) = (0, 0)
	while k > 0:
		if (a, b) in cycle123:
			if k % cycle123[a, b][0] == 0:
				ansA += cycle123[a, b][1] * k // cycle123[a, b][0]
				ansB += cycle123[a, b][2] * k // cycle123[a, b][0]
				return (ansA, ansB)
		k -= 1
		ansA += int(point123[a, b] == 'Alice')
		ansB += int(point123[a, b] == 'Bob')
		(a, b) = map123[a, b]
	return (ansA, ansB)
(ansa, ansb) = solve()
print(ansa, ansb)


(k, a, b) = [int(i) for i in input().split()]
al = [[int(i) for i in input().split()] for j in range(3)]
bb = [[int(i) for i in input().split()] for j in range(3)]
(d, sa, sb, cr) = (dict(), 0, 0, 0)
mat = [[(0, 0), (0, 1), (1, 0)], [(1, 0), (0, 0), (0, 1)], [(0, 1), (1, 0), (0, 0)]]
while cr < k:
	if (a, b) in list(d.keys()):
		break
	cr += 1
	d[a, b] = (sa, sb, cr)
	sc = mat[a - 1][b - 1]
	sa += sc[0]
	sb += sc[1]
	(a, b) = (al[a - 1][b - 1], bb[a - 1][b - 1])
if cr == k:
	print(sa, sb)
	return
ln = cr - d[a, b][2] + 1
adda = sa - d[a, b][0]
addb = sb - d[a, b][1]
left = k - cr
cn = left // ln
sa += cn * adda
sb += cn * addb
cr += ln * cn
while cr < k:
	cr += 1
	sc = mat[a - 1][b - 1]
	sa += sc[0]
	sb += sc[1]
	(a, b) = (al[a - 1][b - 1], bb[a - 1][b - 1])
print(sa, sb)


f = lambda : list(map(int, input().split()))
g = lambda : [[0] * 4] + [[0] + f() for i in range(3)]
h = lambda x, y: x - 1 == y % 3
t = lambda a, b, u, v: (A[a][b], B[a][b], u + h(a, b), v + h(b, a))
(k, a, b) = f()
p = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9
(s, d) = divmod(k, p)
(A, B) = (g(), g())
u = v = x = y = 0
for j in range(d):
	(a, b, u, v) = t(a, b, u, v)
for i in range(p):
	(a, b, x, y) = t(a, b, x, y)
print(u + x * s, v + y * s)


def readln():
	return map(int, input().rstrip().split())

def score_of(x, y):
	if x == y:
		return (0, 0)
	elif x == 3 and y == 2:
		return (1, 0)
	elif x == 2 and y == 3:
		return (0, 1)
	elif x == 2 and y == 1:
		return (1, 0)
	elif x == 1 and y == 2:
		return (0, 1)
	elif x == 1 and y == 3:
		return (1, 0)
	elif x == 3 and y == 1:
		return (0, 1)
(k, a, b) = readln()
(A, B) = ([[0 for i in range(4)] for j in range(4)], [[0 for i in range(4)] for j in range(4)])
for i in range(1, 4):
	(a1, a2, a3) = readln()
	(A[i][1], A[i][2], A[i][3]) = (a1, a2, a3)
for i in range(1, 4):
	(a1, a2, a3) = readln()
	(B[i][1], B[i][2], B[i][3]) = (a1, a2, a3)
circle = set()
circle_score = [(0, 0)]
circle_data = []
(x, y) = (a, b)
while (x, y) not in circle and k > 0:
	circle.add((x, y))
	circle_data.append((x, y))
	(x_, y_) = score_of(x, y)
	circle_score.append((circle_score[-1][0] + x_, circle_score[-1][1] + y_))
	(x, y) = (A[x][y], B[x][y])
	k -= 1
if k == 0:
	print('{} {}'.format(circle_score[-1][0], circle_score[-1][1]))
	exit()
pre_idx = circle_data.index((x, y))
freq = len(circle_data) - pre_idx
(fscorea, fscoreb) = (circle_score[-1][0] - circle_score[pre_idx][0], circle_score[-1][1] - circle_score[pre_idx][1])
p = k // freq
rsa = circle_score[-1][0] + fscorea * p
rsb = circle_score[-1][1] + fscoreb * p
r = k % freq
while r > 0:
	(x_, y_) = score_of(x, y)
	rsa += x_
	rsb += y_
	(x, y) = (A[x][y], B[x][y])
	r -= 1
print('{} {}'.format(rsa, rsb))


def update_point(a_point, b_point, winner):
	if winner == 'a':
		a_point += 1
	elif winner == 'b':
		b_point += 1
	return (a_point, b_point)
(k, a, b) = map(int, input().split())
A = [None] * 3
for i in range(3):
	A[i] = list(map(int, input().split()))
B = [None] * 3
for i in range(3):
	B[i] = list(map(int, input().split()))
winner_rules = [None] * 3
for i in range(3):
	winner_rules[i] = ['='] * 3
winner_rules[2][1] = winner_rules[1][0] = winner_rules[0][2] = 'a'
winner_rules[1][2] = winner_rules[0][1] = winner_rules[2][0] = 'b'
a_point = b_point = first_a_point = first_b_point = 0
winner = winner_rules[a - 1][b - 1]
(first_a_point, first_b_point) = update_point(first_a_point, first_b_point, winner)
results = []
is_found_loop = False
pos_found = -1
for i in range(k - 1):
	a_new = A[a - 1][b - 1]
	b_new = B[a - 1][b - 1]
	a = a_new
	b = b_new
	winner = winner_rules[a - 1][b - 1]
	(a_point, b_point) = update_point(a_point, b_point, winner)
	result = str(a) + str(b) + winner
	if result in results:
		pos_found = results.index(result)
		is_found_loop = True
		break
	results.append(result)
if is_found_loop:
	a_point = b_point = 0
	for i in range(pos_found):
		winner = results[i][-1]
		(a_point, b_point) = update_point(a_point, b_point, winner)
	results = results[pos_found:]
	len_results = len(results)
	remain = k - 1 - pos_found
	div = remain // len_results
	a_point_loop = b_point_loop = 0
	for i in range(len_results):
		winner = results[i][-1]
		(a_point_loop, b_point_loop) = update_point(a_point_loop, b_point_loop, winner)
	a_point_loop *= div
	b_point_loop *= div
	mod = remain % len_results
	for i in range(mod):
		winner = results[i][-1]
		(a_point_loop, b_point_loop) = update_point(a_point_loop, b_point_loop, winner)
	a_point += a_point_loop
	b_point += b_point_loop
print(a_point + first_a_point, b_point + first_b_point)


def winner(q, r):
	if q == r:
		return 'd'
	if q == 0 and r == 2:
		return 'a'
	if r == 0 and q == 2:
		return 'b'
	if q > r:
		return 'a'
	return 'b'
(k, a, b) = [int(x) - 1 for x in input().split()]
A = [[int(x) - 1 for x in input().split()] for y in range(3)]
B = [[int(x) - 1 for x in input().split()] for z in range(3)]
score = {'a': 0, 'b': 0, 'd': 0}
score[winner(a, b)] += 1
d = {(a, b): 0}
i = 1
j = -1
while k:
	na = A[a][b]
	nb = B[a][b]
	if (na, nb) in list(d.keys()):
		j = d[na, nb]
		break
	else:
		d[na, nb] = i
		score[winner(na, nb)] += 1
		a = na
		b = nb
	i += 1
	k -= 1
if j == -1:
	print(score['a'], score['b'])
	return
period = {'a': 0, 'b': 0, 'd': 0}
end_score = {'a': 0, 'b': 0, 'd': 0}
rem = k % (i - j)
per = k // (i - j)
for move in d:
	if d[move] < j:
		continue
	if d[move] < j + rem:
		end_score[winner(move[0], move[1])] += 1
	period[winner(move[0], move[1])] += 1
for win in ['a', 'b', 'd']:
	score[win] += end_score[win] + per * period[win]
print(score['a'], score['b'])


def getscore(ca, cb):
	if ca == cb:
		return (0, 0)
	if ca == 2 and cb == 1:
		return (1, 0)
	if ca == 1 and cb == 0:
		return (1, 0)
	if ca == 0 and cb == 2:
		return (1, 0)
	return (0, 1)
(k, a, b) = map(int, input().split())
a -= 1
b -= 1
alisa = [[*map(lambda x: int(x) - 1, input().split())] for _ in range(3)]
bob = [[*map(lambda x: int(x) - 1, input().split())] for _ in range(3)]
vis = [[-1] * 3 for _ in range(3)]
aliscore = [0] * 14
bobscore = [0] * 14
for i in range(k):
	if vis[a][b] != -1:
		loop = i - vis[a][b]
		bobloopscore = bobscore[i] - bobscore[vis[a][b]]
		alisaloopscore = aliscore[i] - aliscore[vis[a][b]]
		ansa = ansb = 0
		ost = k - vis[a][b]
		ansa += aliscore[vis[a][b]]
		ansb += bobscore[vis[a][b]]
		ansa += alisaloopscore * (ost // loop)
		ansb += bobloopscore * (ost // loop)
		for _ in range(ost % loop):
			(sca, scb) = getscore(a, b)
			ansa += sca
			ansb += scb
			(a, b) = (alisa[a][b], bob[a][b])
		print(ansa, ansb)
		exit()
	else:
		vis[a][b] = i
	(sca, scb) = getscore(a, b)
	aliscore[i + 1] = aliscore[i] + sca
	bobscore[i + 1] = bobscore[i] + scb
	(a, b) = (alisa[a][b], bob[a][b])
print(aliscore[k], bobscore[k])


def get_ints():
	return [int(n) for n in input().split()]

def move(matrix, a, b):
	return matrix[a][b]

def game(a, b):
	res = (a - b) % 3
	if res == 1:
		return (1, 0)
	elif res == 2:
		return (0, 1)
	else:
		return (0, 0)
z = get_ints()
assert len(z) == 3
(k, a, b) = (z[0], z[1] - 1, z[2] - 1)
alice = list()
for i in range(3):
	z = get_ints()
	assert len(z) == 3
	for j in range(len(z)):
		z[j] -= 1
	alice.append(z.copy())
bob = list()
for i in range(3):
	z = get_ints()
	assert len(z) == 3
	for j in range(len(z)):
		z[j] -= 1
	bob.append(z.copy())
moves = list()
results = list()
index = -1
for m in range(k):
	if (a, b) in moves:
		index = moves.index((a, b))
		break
	moves.append((a, b))
	results.append(game(a, b))
	(a, b) = (move(alice, a, b), move(bob, a, b))
(apoint, bpoint) = (0, 0)
c = len(moves) - index
rep = (k - index) // c
for res in results[index:]:
	(da, db) = res
	apoint += da
	bpoint += db
apoint *= rep
bpoint *= rep
rem = (k - index) % c
for i in range(rem):
	(da, db) = results[i + index]
	apoint += da
	bpoint += db
for res in results[:index]:
	(da, db) = res
	apoint += da
	bpoint += db
print(apoint, bpoint)


f = lambda : list(map(int, input().split()))
g = lambda : [[0] * 4] + [[0] + f() for i in range(3)]
h = lambda x, y: x - 1 == y % 3
t = lambda a, b, u, v: (A[a][b], B[a][b], u + h(a, b), v + h(b, a))
(k, a, b) = f()
p = 2520
(s, d) = (k // p, k % p)
if s:
	(s, d) = (s - 1, d + p)
(A, B) = (g(), g())
u = v = x = y = 0
for j in range(d):
	(a, b, u, v) = t(a, b, u, v)
for i in range(p):
	(a, b, x, y) = t(a, b, x, y)
print(u + x * s, v + y * s)


[k, a, b] = list(map(int, input().split()))
A = list()
for i in range(3):
	A.append(list(map(int, input().split())))
B = list()
for i in range(3):
	B.append(list(map(int, input().split())))
a -= 1
b -= 1
(sa, sb) = (0, 0)
vis = [[0, 0, 0] for i in range(9)]
x = a + 3 * b
step = 1
cycle = False
while step <= k:
	(a, b) = (x % 3, x // 3)
	if a < b:
		if a == 0 and b == 2:
			sa += 1
		else:
			sb += 1
	elif a > b:
		if b == 0 and a == 2:
			sb += 1
		else:
			sa += 1
	if not cycle:
		if vis[x][0] == 0:
			vis[x] = [step, sa, sb]
		else:
			len = step - vis[x][0]
			cnt = (k - step) // len
			step += cnt * len
			s_diff = [sa - vis[x][1], sb - vis[x][2]]
			[sa, sb] = [sa + cnt * s_diff[0], sb + cnt * s_diff[1]]
			cycle = True
	step += 1
	(a2, b2) = (A[a][b], B[a][b])
	a2 -= 1
	b2 -= 1
	x = a2 + 3 * b2
print(sa, sb)


from collections import defaultdict
(alice, bob) = (0, 0)

def put():
	return map(int, input().split())

def play(a, b):
	global alice, bob
	if a == b:
		pass
	elif (a, b) in [(2, 1), (1, 0), (0, 2)]:
		alice += 1
	else:
		bob += 1
(k, a, b) = put()
(A, B) = ([list(put()) for i in range(3)], [list(put()) for i in range(3)])
(a, b, c, d) = (a - 1, b - 1, -1, -1)
vis = defaultdict()
(i, t, found) = (1, k, False)
while t > 0:
	play(a, b)
	if not found and (a, b, c, d) in vis:
		(ai, bi, ii) = vis[a, b, c, d]
		size = i - ii
		multi = 1 + (k - i) // size
		alice = (alice - ai) * multi + ai
		bob = (bob - bi) * multi + bi
		t = 1 + (k - i) % size
		found = True
	vis[a, b, c, d] = (alice, bob, i)
	(a, b, c, d) = (A[a][b] - 1, B[a][b] - 1, a, b)
	t -= 1
	i += 1
print(alice, bob)


(k, a, b) = map(int, input().split(' '))
mas = []
hm = []
hmhm = []
for j in range(2):
	mas.append([])
	for i in range(3):
		mas[j].append(list(map(int, input().split(' '))))
aa = 0
bb = 0
hmhm.append((aa, bb))
if a != b:
	if a == 2 and b == 1 or (a == 3 and b == 2) or (a == 1 and b == 3):
		aa += 1
	else:
		bb += 1
hm.append((a, b))
hmhm.append((aa, bb))
i = 0
while i < k - 1 and (mas[0][a - 1][b - 1], mas[1][a - 1][b - 1]) not in hm:
	t = mas[0][a - 1][b - 1]
	y = mas[1][a - 1][b - 1]
	a = t
	b = y
	if a != b:
		if a == 2 and b == 1 or (a == 3 and b == 2) or (a == 1 and b == 3):
			aa += 1
		else:
			bb += 1
	hm.append((a, b))
	hmhm.append((aa, bb))
	i += 1
if i < k - 1:
	i = hm.index((mas[0][a - 1][b - 1], mas[1][a - 1][b - 1]))
	aa += ((k - i) // (len(hm) - i) - 1) * (hmhm[-1][0] - hmhm[0 if i == 0 else i][0])
	bb += ((k - i) // (len(hm) - i) - 1) * (hmhm[-1][1] - hmhm[0 if i == 0 else i][1])
	if (k - i) % (len(hm) - i) != 0:
		aa += hmhm[(k - i) % (len(hm) - i) - 1 + (0 if i == 0 else 1) + max(1, i)][0] - hmhm[0 if i == 0 else i][0]
		bb += hmhm[(k - i) % (len(hm) - i) - 1 + (0 if i == 0 else 1) + max(1, i)][1] - hmhm[0 if i == 0 else i][1]
print(' '.join((str(aa), str(bb))))


(k, a0, b0) = map(int, input().split())
(a0, b0) = (a0 - 1, b0 - 1)
A = tuple((tuple(map(lambda c: int(c) - 1, input().split())) for _ in range(3)))
B = tuple((tuple(map(lambda c: int(c) - 1, input().split())) for _ in range(3)))
score = lambda a, b: (int((a - b) % 3 == 1), int((b - a) % 3 == 1))
S = set()
cur = (a0, b0)
while cur not in S:
	S.add(cur)
	(a, b) = cur
	cur = (A[a][b], B[a][b])
pre = (0, 0)
p = (a0, b0)
while k > 0 and p != cur:
	pre = tuple((c + d for (c, d) in zip(pre, score(*p))))
	(a, b) = p
	p = (A[a][b], B[a][b])
	k -= 1
if k == 0:
	print(*pre)
	return
L = 1
cnt = score(*cur)
(a, b) = cur
p = (A[a][b], B[a][b])
while p != cur:
	cnt = tuple((c + d for (c, d) in zip(cnt, score(*p))))
	L += 1
	(a, b) = p
	p = (A[a][b], B[a][b])
n = k // L
k -= k // L * L
cnt = (cnt[0] * n, cnt[1] * n)
suf = (0, 0)
p = cur
while k > 0:
	suf = tuple((c + d for (c, d) in zip(suf, score(*p))))
	(a, b) = p
	p = (A[a][b], B[a][b])
	k -= 1
print(*(a + b + c for (a, b, c) in zip(pre, cnt, suf)))


from functools import reduce

def get_scores(a, b):
	if a == b:
		return (0, 0)
	elif (a, b) in ((2, 1), (1, 0), (0, 2)):
		return (1, 0)
	else:
		return (0, 1)

def add(t1, t2):
	return tuple(map(sum, list(zip(t1, t2))))

def mul(t, x):
	return (t[0] * x, t[1] * x)
(k, a, b) = list(map(int, input().split(' ')))
alice_ts = {}
bob_ts = {}
for ts in (alice_ts, bob_ts):
	for i in range(3):
		xs = list(map(int, input().split(' ')))[::-1]
		for j in range(3):
			ts[i, j] = xs.pop() - 1
state = (a - 1, b - 1)
result = (0, 0)
log = {}
step_results = []
(start, end) = (None, None)
for i in range(k):
	if state in log:
		start = log[state]
		end = i
		break
	current_res = get_scores(*state)
	result = add(result, current_res)
	log[state] = i
	step_results.append(current_res)
	if end is not None:
		break
	state = (alice_ts[state], bob_ts[state])
if end is not None:
	cycle_sum = reduce(add, step_results[start:end])
	total_cycle_sum = mul(cycle_sum, (k - end) // (end - start))
	result = add(result, total_cycle_sum)
	for i in range(0, (k - end) % (end - start)):
		result = add(result, step_results[start + i])
print(result[0], result[1])


def winner(x, y):
	if x == 1 and y == 3:
		return (1, 0)
	if x == 2 and y == 1:
		return (1, 0)
	if x == 3 and y == 2:
		return (1, 0)
	if x == y:
		return (0, 0)
	if x == 3 and y == 1:
		return (0, 1)
	if x == 1 and y == 2:
		return (0, 1)
	if x == 2 and y == 3:
		return (0, 1)
(k, a, b) = map(int, input().split())
choice_a = []
choice_b = []
for i in range(3):
	choice_a.append(list(map(int, input().split())))
for i in range(3):
	choice_b.append(list(map(int, input().split())))
line = []
win_line = []
line.append((a - 1, b - 1))
win_line.append(winner(a, b))
while True:
	(x, y) = line[-1]
	xc = choice_a[x][y] - 1
	yc = choice_b[x][y] - 1
	if (xc, yc) in line:
		s = line.index((xc, yc))
		mod = len(line) - s
		s_cnt = win_line[s]
		s_mod = (win_line[-1][0] - win_line[s][0], win_line[-1][1] - win_line[s][1])
		break
	line.append((xc, yc))
	ans = winner(xc + 1, yc + 1)
	win_line.append((win_line[-1][0] + ans[0], win_line[-1][1] + ans[1]))
if k < s:
	print(*win_line[k - 1])
else:
	kk = (k - s - 1) % mod
	km = (k - s - 1) // mod
	ans1 = win_line[kk + s]
	ans2 = win_line[-1]
	win_line.append((0, 0))
	ans3 = win_line[s - 1]
	ans_mod = (ans2[0] - ans3[0], ans2[1] - ans3[1])
	print(ans1[0] + ans_mod[0] * km, ans1[1] + ans_mod[1] * km)


def find(a, b):
	for i in range(9):
		if aliceK[i][0] == a and aliceK[i][1] == b:
			return [aliceA[i], bobA[i]]

def winner(m, n):
	if m == n:
		return 0
	if m == 3 and n == 2:
		return 1
	elif m == 2 and n == 1:
		return 1
	elif m == 1 and n == 3:
		return 1
	else:
		return 2
(kk, a, b) = map(int, input().split())
aliceK = [[], [], [], [], [], [], [], [], []]
aliceA = []
bobK = [[], [], [], [], [], [], [], [], []]
bobA = []
k = int(0)
for i in range(3):
	n = list(map(int, input().split()))
	for j in range(3):
		aliceK[k].append(i + 1)
		aliceK[k].append(j + 1)
		aliceA.append(n[j])
		k += 1
k = 0
for i in range(3):
	n = list(map(int, input().split()))
	for j in range(3):
		bobK[k].append(i + 1)
		bobK[k].append(j + 1)
		bobA.append(n[j])
		k += 1
used = []
used.append([a, b])
for i in range(9):
	l = find(a, b)
	used.append(l)
	a = l[0]
	b = l[1]
lenC = 0
start = 0
finish = 0
flag = 0
for i in range(9):
	for j in range(i + 1, 10):
		if used[i][0] == used[j][0] and used[i][1] == used[j][1]:
			lenC = j - i
			start = i
			finish = j
			flag = 1
			break
	if flag == 1:
		break
moneyCA = 0
moneyCB = 0
for i in range(start, finish):
	winnerr = winner(used[i][0], used[i][1])
	if winnerr == 1:
		moneyCA += 1
	elif winnerr == 2:
		moneyCB += 1
AllMoneyA = 0
AllMoneyB = 0
for i in range(start):
	winnerr = winner(used[i][0], used[i][1])
	if winnerr == 1:
		AllMoneyA += 1
	elif winnerr == 2:
		AllMoneyB += 1
if kk <= lenC:
	AllMoneyA = 0
	AllNoneyB = 0
	for i in range(kk):
		winnerr = winner(used[i][0], used[i][1])
		if winnerr == 1:
			AllMoneyA += 1
		elif winnerr == 2:
			AllMoneyB += 1
	print(AllMoneyA, AllMoneyB)
	exit()
AllMoneyA += (kk - start) // lenC * moneyCA
AllMoneyB += (kk - start) // lenC * moneyCB
dopZopa = kk - start - (kk - start) // lenC * lenC
for i in range(start, dopZopa + start):
	winnerr = winner(used[i][0], used[i][1])
	if winnerr == 1:
		AllMoneyA += 1
	elif winnerr == 2:
		AllMoneyB += 1
print(AllMoneyA, AllMoneyB)


import sys

def log(s):
	pass
(k, a, b) = map(int, sys.stdin.readline().strip().split(' '))
alice = {}
for i in range(3):
	(one, two, three) = map(int, sys.stdin.readline().strip().split(' '))
	alice[i + 1, 1] = one
	alice[i + 1, 2] = two
	alice[i + 1, 3] = three
bob = {}
for i in range(3):
	(one, two, three) = map(int, sys.stdin.readline().strip().split(' '))
	bob[i + 1, 1] = one
	bob[i + 1, 2] = two
	bob[i + 1, 3] = three
score = {(1, 1): (0, 0), (2, 2): (0, 0), (3, 3): (0, 0), (1, 2): (0, +1), (2, 3): (0, +1), (3, 1): (0, 1), (2, 1): (1, 0), (3, 2): (1, 0), (1, 3): (1, 0)}
seen = []
current = (a, b)
while current not in seen:
	seen.append(current)
	current = (alice[current], bob[current])
loop = seen.index(current)
vals = [score[state] for state in seen]
(a_point, b_point) = (0, 0)
(a_vals, b_vals) = zip(*vals)
a_point += sum(a_vals[:min(k, loop)])
b_point += sum(b_vals[:min(k, loop)])
k = k - loop
loop_len = len(seen) - loop
a_loop_val = sum(a_vals[loop:])
b_loop_val = sum(b_vals[loop:])
if k >= 0:
	tail = k % loop_len
	num_iters = k // loop_len
	a_point += sum(a_vals[loop:loop + tail])
	b_point += sum(b_vals[loop:loop + tail])
	a_point += num_iters * a_loop_val
	b_point += num_iters * b_loop_val
log(a_point)
log(b_point)
print(round(a_point), round(b_point))


x = [[0 for i in range(4)] for j in range(4)]
y = [[0 for i in range(4)] for j in range(4)]
vis = [[0 for i in range(4)] for j in range(4)]
dic = {}
(k, a, b) = list(map(lambda _: int(_), input().split()))
for i in range(3):
	(x[i + 1][1], x[i + 1][2], x[i + 1][3]) = list(map(lambda _: int(_), input().split()))
for i in range(3):
	(y[i + 1][1], y[i + 1][2], y[i + 1][3]) = list(map(lambda _: int(_), input().split()))
s1 = s2 = 0
dic[1] = [s1, s2]
xx = a - b
yy = b - a
if xx == -2 or xx == 1:
	xx = 1
else:
	xx = 0
if yy == -2 or yy == 1:
	yy = 1
else:
	yy = 0
s1 += xx
s2 += yy
vis[a][b] = 1
k -= 1
cnt = 1
a1 = a
b1 = b
while True and k > 0:
	aa = a1
	bb = b1
	a1 = x[aa][bb]
	b1 = y[aa][bb]
	cnt += 1
	if vis[a1][b1] != 0:
		tmp = vis[a1][b1]
		break
	vis[a1][b1] = cnt
	dic[cnt] = [s1, s2]
	xx = a1 - b1
	yy = b1 - a1
	if xx == -2 or xx == 1:
		xx = 1
	else:
		xx = 0
	if yy == -2 or yy == 1:
		yy = 1
	else:
		yy = 0
	s1 += xx
	s2 += yy
	k -= 1
if k == 0:
	print(s1, s2)
else:
	ans1 = s1
	ans2 = s2
	s1 -= dic[tmp][0]
	s2 -= dic[tmp][1]
	cnt -= tmp
	ans1 += k // cnt * s1
	ans2 += k // cnt * s2
	k %= cnt
	while k > 0:
		xx = a1 - b1
		yy = b1 - a1
		if xx == -2 or xx == 1:
			xx = 1
		else:
			xx = 0
		if yy == -2 or yy == 1:
			yy = 1
		else:
			yy = 0
		ans1 += xx
		ans2 += yy
		aa = a1
		bb = b1
		a1 = x[aa][bb]
		b1 = y[aa][bb]
		k -= 1
	print(ans1, ans2)


from decimal import *
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
getcontext().prec = 25
(k, a, b) = [int(i) for i in input().split(' ')]
A = []
B = []
ap = 0
bp = 0
ac = a
bc = b
ah = []
bh = []
sh = []
alert = 0
for i in range(3):
	A.append(input())
for i in range(3):
	B.append(input())

def Alice_choice(i, j):
	return int(A[i - 1][2 * j - 2])

def Bob_choice(i, j):
	return int(B[i - 1][2 * j - 2])
if ac == bc + 1 or ac == bc - 2:
	ap += 1
	ah.append(ac)
	bh.append(bc)
	sh.append(1)
elif bc == ac + 1 or bc == ac - 2:
	bp += 1
	ah.append(ac)
	bh.append(bc)
	sh.append(-1)
else:
	ah.append(ac)
	bh.append(bc)
	sh.append(0)
q = 0
r = 0
for q in range(k - 1):
	temp_a = Alice_choice(ac, bc)
	temp_b = Bob_choice(ac, bc)
	ac = temp_a
	bc = temp_b
	if ac == bc + 1 or ac == bc - 2:
		ap += 1
		ah.append(ac)
		bh.append(bc)
		sh.append(1)
	elif bc == ac + 1 or bc == ac - 2:
		bp += 1
		ah.append(ac)
		bh.append(bc)
		sh.append(-1)
	else:
		ah.append(ac)
		bh.append(bc)
		sh.append(0)
	for r in range(q + 1):
		if ac == ah[r] and bc == bh[r]:
			alert = 1
			break
	if alert == 1:
		break
q += 1
period = q - r
score_pp = sh[r + 1:q + 1]
end = (k - len(sh)) % period
repeat = Decimal(k - len(sh) - end) / period
aadd = 0
badd = 0
for i in score_pp:
	if i == 1:
		aadd += 1
	elif i == -1:
		badd += 1
ap += aadd * repeat
bp += badd * repeat
for i in score_pp[:end]:
	if i == 1:
		ap += 1
	elif i == -1:
		bp += 1
print(ap, end=' ')
print(bp)


def winner(A, B):
	if A == B:
		return -1
	if A == 1 and B != 2:
		return 0
	if A == 2 and B != 3:
		return 0
	if A == 3 and B != 1:
		return 0
	if A != 2 and B == 1:
		return 1
	if A != 3 and B == 2:
		return 1
	if A != 1 and B == 3:
		return 1
	return -1
(k, A, B) = map(int, input().split())
arr = [0] * 3
brr = [0] * 3
for i in range(3):
	arr[i] = list(map(int, input().split()))
for i in range(3):
	brr[i] = list(map(int, input().split()))
q = [0] * 81
p = [0] * 81
w = [-1] * 81
q[0] = A
p[0] = B
w[0] = winner(A, B)
j = 1
flag = 1
while flag:
	for i in range(81):
		if arr[q[j - 1] - 1][p[j - 1] - 1] == q[i] and brr[q[j - 1] - 1][p[j - 1] - 1] == p[i]:
			flag = 0
			ans = i
	if flag:
		q[j] = arr[q[j - 1] - 1][p[j - 1] - 1]
		p[j] = brr[q[j - 1] - 1][p[j - 1] - 1]
		w[j] = winner(q[j], p[j])
		j += 1
ze = 0
on = 0
for i in range(ans, 81):
	if w[i] == 0:
		ze += 1
	if w[i] == 1:
		on += 1
r = (k - ans) % (j - ans)
d = (k - ans) // (j - ans)
if d < 0:
	d = 0
	r = 0
ze = ze * d
on = on * d
for i in range(min(ans, k)):
	if w[i] == 0:
		ze += 1
	if w[i] == 1:
		on += 1
i = ans
while r:
	if w[i] == 0:
		ze += 1
	if w[i] == 1:
		on += 1
	i += 1
	r -= 1
print(ze, on)


(k, a, b) = map(int, input().split())
A = []
for _ in range(3):
	A += [list(map(lambda x: int(x) - 1, input().split()))]
B = []
for _ in range(3):
	B += [list(map(lambda x: int(x) - 1, input().split()))]
S = [[(0, 0), (0, 1), (1, 0)], [(1, 0), (0, 0), (0, 1)], [(0, 1), (1, 0), (0, 0)]]
(a, b) = (a - 1, b - 1)
C = {}
(a_s, b_s, i) = (0, 0, 0)
while k > 0 and (a, b) not in C:
	k -= 1
	C[a, b] = (a_s, b_s, i)
	tmp = S[a][b]
	a_s += tmp[0]
	b_s += tmp[1]
	(a, b) = (A[a][b], B[a][b])
	i += 1
if k > 0:
	p = i - C[a, b][2]
	m = k // p
	a_s += m * (a_s - C[a, b][0])
	b_s += m * (b_s - C[a, b][1])
	k %= p
	while k > 0:
		k -= 1
		tmp = S[a][b]
		a_s += tmp[0]
		b_s += tmp[1]
		(a, b) = (A[a][b], B[a][b])
print(str(a_s) + ' ' + str(b_s))


(k, a, b) = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(3)]
B = [list(map(int, input().split())) for _ in range(3)]
wina = winb = ca = cb = 0
ls = [(0, 0)]
dic = {}
pre = k
cycle = k
for i in range(k):
	if (a - b + 3) % 3 == 1:
		wina += 1
	elif (a - b + 3) % 3 == 2:
		winb += 1
	ls.append((wina, winb))
	if (a, b) in dic:
		pre = dic[a, b]
		cycle = i - pre
		ca = wina - ls[pre + 1][0]
		cb = winb - ls[pre + 1][1]
		break
	dic[a, b] = i
	(a, b) = (A[a - 1][b - 1], B[a - 1][b - 1])
wina = ls[(k - pre) % cycle + pre][0] + ca * ((k - pre) // cycle)
winb = ls[(k - pre) % cycle + pre][1] + cb * ((k - pre) // cycle)
print('%d %d' % (wina, winb))


def read():
	return list(map(int, input().split(' ')))
(k, a, b) = read()
A = [read() for _ in range(3)]
B = [read() for _ in range(3)]
(ra, rb) = (0, 0)
skipped = False
pos = {(a, b): 0}
scoreA = {(a, b): 0}
scoreB = {(a, b): 0}
if (b - a) % 3 == 1:
	rb = 1
elif (b - a) % 3 == 2:
	ra = 1
i = 1
while i < k:
	na = A[a - 1][b - 1]
	nb = B[a - 1][b - 1]
	if (na, nb) in pos and (not skipped):
		cycleLen = i - pos[na, nb]
		cycleScoreA = ra - scoreA[na, nb]
		cycleScoreB = rb - scoreB[na, nb]
		skipCycles = (k - i) // cycleLen
		ra += cycleScoreA * skipCycles
		rb += cycleScoreB * skipCycles
		i += skipCycles * cycleLen
		skipped = True
	else:
		scoreA[na, nb] = ra
		scoreB[na, nb] = rb
		pos[na, nb] = i
		if (nb - na) % 3 == 1:
			rb += 1
		elif (nb - na) % 3 == 2:
			ra += 1
		i += 1
		(a, b) = (na, nb)
print(ra, rb)


class Player:
	__slots__ = ['choices']

	def __init__(self, choices):
		self.choices = choices

	def choose_next(self, row, col):
		return self.choices[row - 1][col - 1]

	def round_score(own, other):
		if own == other + 1 or (own == 1 and other == 3):
			return 1
		return 0

class State:
	__slots__ = ['score_a', 'score_b', 'time']

	def __init__(self, a, b, time):
		self.score_a = a
		self.score_b = b
		self.time = time

def read_player():
	choices = [list(map(int, input().split())) for i in range(3)]
	return Player(choices)

def play(rounds, pa, a, pb, b):
	memo = {}
	score = [0, 0]
	cacat = False
	for i in range(rounds):
		pair = (a, b)
		if pair in memo:
			cycle_len = i - memo[pair].time
			cycles = (rounds - i) // cycle_len
			score_a = score[0] - memo[pair].score_a
			score_b = score[1] - memo[pair].score_b
			score[0] += score_a * cycles
			score[1] += score_b * cycles
			rounds -= i + cycles * cycle_len
			cacat = True
			break
		else:
			memo[pair] = State(score[0], score[1], i)
		score[0] += Player.round_score(a, b)
		score[1] += Player.round_score(b, a)
		(a, b) = (pa.choose_next(a, b), pb.choose_next(a, b))
	if cacat:
		for i in range(rounds):
			score[0] += Player.round_score(a, b)
			score[1] += Player.round_score(b, a)
			(a, b) = (pa.choose_next(a, b), pb.choose_next(a, b))
	return score

def main():
	(rounds, a, b) = map(int, input().split())
	pa = read_player()
	pb = read_player()
	score = play(rounds, pa, a, pb, b)
	print(score[0], score[1])
main()


(k, a, b) = map(int, input().split())
A = []
B = []
(sa, sb) = (0, 0)
r = {(1, 1): (0, 0), (1, 2): (0, 1), (1, 3): (1, 0), (2, 1): (1, 0), (2, 2): (0, 0), (2, 3): (0, 1), (3, 1): (0, 1), (3, 2): (1, 0), (3, 3): (0, 0)}
p = {}
for _ in range(3):
	A.append(list(map(int, input().split())))
for _ in range(3):
	B.append(list(map(int, input().split())))
i = 0
while i < k:
	if (a, b) in p:
		(j, psa, psb) = p[a, b]
		l = i - j
		psa = sa - psa
		psb = sb - psb
		rep = (k - i) // l
		sa += psa * rep
		sb += psb * rep
		i += l * rep
		break
	else:
		p[a, b] = (i, sa, sb)
	t = r[a, b]
	sa += t[0]
	sb += t[1]
	(a, b) = (A[a - 1][b - 1], B[a - 1][b - 1])
	i += 1
while i < k:
	t = r[a, b]
	sa += t[0]
	sb += t[1]
	(a, b) = (A[a - 1][b - 1], B[a - 1][b - 1])
	i += 1
print(sa, sb)


(k, a, b) = map(int, input().split())
am = {}
bm = {}
d = []
alisa = 0
bob = 0
a1 = 0
for i in range(3):
	am[i] = list(map(int, input().split()))
for i in range(3):
	bm[i] = list(map(int, input().split()))
i = 0
last = 0
while i != k:
	a -= 1
	b -= 1
	if not str(a) + str(b) in d:
		d.append(str(a) + str(b))
		a1 = am[a][b]
		b = bm[a][b]
		a = a1
	else:
		last = str(a) + str(b)
		break
	i += 1
j = 0
o = 0
if last == 0:
	while j != k:
		if d[j] == '01':
			bob += 1
		elif d[j] == '10':
			alisa += 1
		elif d[j] == '02':
			alisa += 1
		elif d[j] == '20':
			bob += 1
		elif d[j] == '12':
			bob += 1
		elif d[j] == '21':
			alisa += 1
		j += 1
	print(alisa, bob)
else:
	while d[j] != last:
		if d[j] == '01':
			bob += 1
		elif d[j] == '10':
			alisa += 1
		elif d[j] == '02':
			alisa += 1
		elif d[j] == '20':
			bob += 1
		elif d[j] == '12':
			bob += 1
		elif d[j] == '21':
			alisa += 1
		j += 1
		o += 1
	k -= o
	while d[0] != last:
		d.pop(0)
	bobc = 0
	alisac = 0
	u = 0
	for j in range(len(d)):
		if d[j] == '01':
			bobc += 1
		elif d[j] == '10':
			alisac += 1
		elif d[j] == '02':
			alisac += 1
		elif d[j] == '20':
			bobc += 1
		elif d[j] == '12':
			bobc += 1
		elif d[j] == '21':
			alisac += 1
	alisac = alisac * (k // len(d))
	bobc = bobc * (k // len(d))
	k -= len(d) * (k // len(d))
	j = 0
	while j != k:
		if d[j] == '01':
			bobc += 1
		elif d[j] == '10':
			alisac += 1
		elif d[j] == '02':
			alisac += 1
		elif d[j] == '20':
			bobc += 1
		elif d[j] == '12':
			bobc += 1
		elif d[j] == '21':
			alisac += 1
		j += 1
	print(alisa + alisac, bob + bobc)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
(k, a, b) = map(int, input().split())
al = []
bo = []
for i in range(3):
	al += [list(map(int, input().split()))]
for i in range(3):
	bo += [list(map(int, input().split()))]

def format(a, b):
	return str(a) + ',' + str(b)

def point(a, b):
	return 1 if [a, b] in [[3, 2], [2, 1], [1, 3]] else 0
oof = [format(a, b)]
st = set()
st.add(oof[0])
loop_after = 0
pts = [point(a, b)]
pts2 = [point(b, a)]
while True:
	(a, b) = (al[a - 1][b - 1], bo[a - 1][b - 1])
	f = format(a, b)
	if f in st:
		loop_after = oof.index(f)
		break
	else:
		pts += [point(a, b)]
		pts2 += [point(b, a)]
		oof += [f]
		st.add(f)
if k <= loop_after:
	print(sum(pts[:k]), sum(pts2[:k]))
	quit()
(p1, p2) = (sum(pts[:loop_after]), sum(pts2[:loop_after]))
pts = pts[loop_after:]
pts2 = pts2[loop_after:]
oof = oof[loop_after:]
k -= loop_after
(s1, s2) = (sum(pts), sum(pts2))
p1 += s1 * (k // len(pts))
p2 += s2 * (k // len(pts2))
k = k % len(pts)
print(p1 + sum(pts[:k]), p2 + sum(pts2[:k]))


(n, a, b) = [int(i) for i in input().split()]
a -= 1
b -= 1
A = []
B = []
for i in range(3):
	A.append([int(i) - 1 for i in input().split()])
for i in range(3):
	B.append([int(i) - 1 for i in input().split()])
(pa, pb) = [0, 0]
seen = [[-1, 0, 0]] * 10
i = 0
while i < n:
	k = a * 3 + b
	if seen[k][0] >= 0:
		t = i - seen[k][0]
		m = (n - 1 - i) // t
		pa += m * (pa - seen[k][1])
		pb += m * (pb - seen[k][2])
		i += t * m
	seen[k] = [i, pa, pb]
	if (a + 1) % 3 == b:
		pb += 1
	if (b + 1) % 3 == a:
		pa += 1
	(a, b) = (A[a][b], B[a][b])
	i += 1
print(pa, pb)


from sys import stdin, stdout
(k, a, b) = map(int, stdin.readline().split())
sze = 3
A = [[]]
B = [[]]
for i in range(sze):
	values = [0] + list(map(int, stdin.readline().split()))
	A.append(values)
for i in range(sze):
	values = [0] + list(map(int, stdin.readline().split()))
	B.append(values)
start = (a, b)
power = 70
steps = [{} for i in range(power)]
cnt = [{} for i in range(power)]
for i in range(1, sze + 1):
	for j in range(1, sze + 1):
		steps[0][i, j] = (A[i][j], B[i][j])
		cnt[0][i, j] = (0, 0)
		if i == 1 and j == 3:
			cnt[0][i, j] = (1, 0)
		if i == 2 and j == 1:
			cnt[0][i, j] = (1, 0)
		if i == 3 and j == 2:
			cnt[0][i, j] = (1, 0)
		if i == 3 and j == 1:
			cnt[0][i, j] = (0, 1)
		if i == 1 and j == 2:
			cnt[0][i, j] = (0, 1)
		if i == 2 and j == 3:
			cnt[0][i, j] = (0, 1)
for z in range(1, power):
	for i in range(1, sze + 1):
		for j in range(1, sze + 1):
			steps[z][i, j] = steps[z - 1][steps[z - 1][i, j]]
			(first, second) = (cnt[z - 1][i, j][0] + cnt[z - 1][steps[z - 1][i, j]][0], cnt[z - 1][i, j][1] + cnt[z - 1][steps[z - 1][i, j]][1])
			cnt[z][i, j] = (first, second)
value = [0, 0]
while k:
	power = 1
	pw = 0
	while power * 2 <= k:
		power *= 2
		pw += 1
	k -= power
	value[0] += cnt[pw][a, b][0]
	value[1] += cnt[pw][a, b][1]
	(a, b) = steps[pw][a, b]
stdout.write(' '.join(list(map(str, value))))


(k, a, b) = map(int, input().split())
A = []
for _ in range(3):
	temp = [int(x) for x in input().split()]
	A.append(temp)
B = []
for _ in range(3):
	temp = [int(x) for x in input().split()]
	B.append(temp)
(flag, ac, bc, cur) = (0, 0, 0, 1)
m = {}
(ans1, ans2) = ([0] * 1000, [0] * 1000)
while cur <= k:
	if (a, b) in m and flag != 1:
		add1 = ans1[cur - 1] - ans1[m[a, b] - 1]
		add2 = ans2[cur - 1] - ans2[m[a, b] - 1]
		l = cur - m[a, b]
		times = (k - (cur - 1)) // l
		ac += add1 * times
		bc += add2 * times
		cur += l * ((k - (cur - 1)) // l)
		cur -= 1
		flag = 1
	else:
		m[a, b] = cur
		if cur <= 500:
			(ans1[cur], ans2[cur]) = (ans1[cur - 1], ans2[cur - 1])
		if a == b % 3 + 1:
			ac += 1
			if cur <= 500:
				ans1[cur] += 1
		if b == a % 3 + 1:
			bc += 1
			if cur <= 500:
				ans2[cur] += 1
		(x, y) = (a - 1, b - 1)
		(a, b) = (A[x][y], B[x][y])
	cur += 1
print(ac, bc)


(k, a, b) = map(int, input().split())
move1 = []
for i in range(3):
	move1.append(list(map(int, input().split())))
move2 = []
for i in range(3):
	move2.append(list(map(int, input().split())))

def points(a, b):
	if a == 1 and b == 2:
		return (0, 1)
	if a == 1 and b == 3:
		return (1, 0)
	if a == 2 and b == 1:
		return (1, 0)
	if a == 2 and b == 3:
		return (0, 1)
	if a == 3 and b == 1:
		return (0, 1)
	if a == 3 and b == 2:
		return (1, 0)
	return (0, 0)
l1 = [(a, b)]
alice = 0
bob = 0
(alice, bob) = (alice + points(a, b)[0], bob + points(a, b)[1])
k -= 1
while k > 0 and 1:
	(a, b) = (l1[-1][0], l1[-1][1])
	(x, y) = (move1[a - 1][b - 1], move2[a - 1][b - 1])
	if (x, y) in l1:
		break
	else:
		k -= 1
		(alice, bob) = (alice + points(x, y)[0], bob + points(x, y)[1])
		l1.append((x, y))
import sys
if k == 0:
	print(alice, bob)
	sys.exit()
for i in range(len(l1)):
	if l1[i] == (x, y):
		break
l1 = l1[i:]
(temp1, temp2) = (0, 0)
for item in l1:
	(temp1, temp2) = (temp1 + points(item[0], item[1])[0], temp2 + points(item[0], item[1])[1])
z = len(l1)
alice += temp1 * (k // z)
bob += temp2 * (k // z)
x = k % z
for item in l1[:x]:
	(alice, bob) = (alice + points(item[0], item[1])[0], bob + points(item[0], item[1])[1])
print(alice, bob)


from math import floor

def kek(a, b):
	if a == b:
		return [0, 0]
	elif a == 3 and b == 1:
		return [0, 1]
	elif a == 1 and b == 3:
		return [1, 0]
	elif a == 1 and b == 2:
		return [0, 1]
	elif a == 2 and b == 1:
		return [1, 0]
	elif a == 2 and b == 3:
		return [0, 1]
	elif a == 3 and b == 2:
		return [1, 0]
(k, a1, b1) = map(int, input().split())
(a, b) = ([], [])
for i in range(3):
	a.append([int(i) for i in input().split()])
for i in range(3):
	b.append([int(i) for i in input().split()])
(n, m) = ([], [])
n.append([a1, b1])
m.append(kek(a1, b1))
i = 0
(ansb, ansa) = (0, 0)
while i != k:
	i += 1
	if i == k:
		break
	(a1, b1) = (a[a1 - 1][b1 - 1], b[a1 - 1][b1 - 1])
	t = [a1, b1]
	if t not in n:
		n.append([a1, b1])
		m.append(kek(a1, b1))
		m[-1][0] += m[-2][0]
		m[-1][1] += m[-2][1]
	else:
		break
if i == k:
	print(*m[-1])
else:
	(ansa, ansb) = (m[-1][0], m[-1][1])
	for j in range(len(n)):
		if n[j] == [a1, b1]:
			t = j
			break
	ror = int((k - i) // (len(n) - t))
	i = i + (len(n) - t) * ror
	if ror != 0:
		if t != 0:
			ansa += (m[-1][0] - m[t - 1][0]) * ror
			ansb += (m[-1][1] - m[t - 1][1]) * ror
		else:
			ansa += m[-1][0] * ror
			ansb += m[-1][1] * ror
	if i != k:
		tmp = kek(a1, b1)
		ansa += tmp[0]
		ansb += tmp[1]
		i += 1
	for j in range(k - i):
		(a1, b1) = (a[a1 - 1][b1 - 1], b[a1 - 1][b1 - 1])
		tmp = kek(a1, b1)
		ansa += tmp[0]
		ansb += tmp[1]
	print(ansa, ansb)


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
(k, x, y) = map(int, input().split())
w = [[0, -1, 1], [1, 0, -1], [-1, 1, 0]]
x -= 1
y -= 1
a = []
b = []
for i in range(3):
	a.append(list(map(lambda x: int(x) - 1, input().split())))
for i in range(3):
	b.append(list(map(lambda x: int(x) - 1, input().split())))
v = [[None for i in range(3)] for i in range(3)]
(alice, bob) = (0, 0)
cycle = 0
while k != 0:
	if v[x][y] == None:
		if w[x][y] == 1:
			alice += 1
		elif w[x][y] == -1:
			bob += 1
		v[x][y] = (alice, bob, cycle)
		k -= 1
		cycle += 1
		(x, y) = (a[x][y], b[x][y])
	else:
		cycle = cycle - v[x][y][2]
		break
(xx, yy) = (x, y)
(at, bt) = (0, 0)
for i in range(cycle):
	if w[xx][yy] == 1:
		at += 1
	elif w[xx][yy] == -1:
		bt += 1
	(xx, yy) = (a[xx][yy], b[xx][yy])
alice += at * (k // cycle)
bob += bt * (k // cycle)
k = k % cycle
while k != 0:
	if w[x][y] == 1:
		alice += 1
	elif w[x][y] == -1:
		bob += 1
	(x, y) = (a[x][y], b[x][y])
	k -= 1
print(alice, bob)


import collections

def do():
	(k, SA, SB) = map(int, input().split(' '))
	a = {}
	b = {}
	for i in range(3):
		for (j, c) in enumerate(input().split(' ')):
			a[i + 1, j + 1] = int(c)
	for i in range(3):
		for (j, c) in enumerate(input().split(' ')):
			b[i + 1, j + 1] = int(c)
	seen = {}

	def put(sa, sb, cur, pa, pb):
		if sa == sb:
			seen[sa, sb] = (0, 0, cur)
		elif (sa, sb) == (1, 2):
			seen[sa, sb] = (0, 1, cur)
		elif (sa, sb) == (1, 3):
			seen[sa, sb] = (1, 0, cur)
		elif (sa, sb) == (2, 3):
			seen[sa, sb] = (0, 1, cur)
		elif (sa, sb) == (2, 1):
			seen[sa, sb] = (1, 0, cur)
		elif (sa, sb) == (3, 1):
			seen[sa, sb] = (0, 1, cur)
		elif (sa, sb) == (3, 2):
			seen[sa, sb] = (1, 0, cur)
		(x, y, z) = seen[sa, sb]
		seen[sa, sb] = (pa + x, pb + y, z)
	put(SA, SB, 1, 0, 0)
	(sa, sb) = (SA, SB)
	(la, lb, _) = seen[sa, sb]
	p = 2
	while p <= k:
		(na, nb) = (a[sa, sb], b[sa, sb])
		if (na, nb) in seen:
			(x, y, z) = seen[na, nb]
			put(na, nb, p, la, lb)
			(nx, ny, nz) = seen[na, nb]
			leng = nz - z
			mod = (k - z) % leng
			count = (k - z) // leng
			(la, lb) = ((nx - x) * count + x, (ny - y) * count + y)
			now = k - mod
			(sa, sb) = (na, nb)
			while now < k:
				(na, nb) = (a[sa, sb], b[sa, sb])
				put(na, nb, now, la, lb)
				now += 1
				(la, lb, _) = seen[na, nb]
				(sa, sb) = (na, nb)
			return (la, lb)
		put(na, nb, p, la, lb)
		p += 1
		(la, lb, _) = seen[na, nb]
		(sa, sb) = (na, nb)
	return (la, lb)
(xx, yy) = do()
print(str(xx) + ' ' + str(yy))


import sys
import math
from collections import defaultdict, deque
(k, a, b) = map(int, sys.stdin.readline().split())
alice = []
for i in range(3):
	(x, y, z) = map(int, sys.stdin.readline().split())
	alice.append([x, y, z])
bob = []
for i in range(3):
	(x, y, z) = map(int, sys.stdin.readline().split())
	bob.append([x, y, z])
q = deque()
q.append([a, b])
vis = defaultdict(int)
vis[a, b] = 1
z = True
(sta, stb) = (a, b)
while q and z:
	(x, y) = q.popleft()
	a = alice[x - 1][y - 1]
	b = bob[x - 1][y - 1]
	if vis[a, b] == 1:
		z = False
		(curx, cury) = (a, b)
	else:
		q.append([a, b])
		vis[a, b] = 1
games = []
if curx == 2 and cury == 1 or (curx == 3 and cury == 2) or (curx == 1 and cury == 3):
	games.append([1, 0])
	al = 1
	bo = 0
elif curx == cury:
	games.append([0, 0])
	al = 0
	bo = 0
else:
	games.append([0, 1])
	al = 0
	bo = 1
q = deque()
q.append([curx, cury])
vis = defaultdict(int)
vis[curx, cury] = 1
while q:
	(x, y) = q.popleft()
	a = alice[x - 1][y - 1]
	b = bob[x - 1][y - 1]
	if vis[a, b] == 1:
		break
	else:
		vis[a, b] = 1
		q.append([a, b])
		if a == 2 and b == 1 or (a == 3 and b == 2) or (a == 1 and b == 3):
			al += 1
			games.append([al, bo])
		elif a == b:
			games.append([al, bo])
		else:
			bo += 1
			games.append([al, bo])
vis = defaultdict(int)
vis[sta, stb] = 1
q = deque()
if sta != curx or stb != cury:
	q.append([sta, stb])
(ga, gb) = (0, 0)
cnt = 0
while q:
	(x, y) = q.popleft()
	if x == 2 and y == 1 or (x == 3 and y == 2) or (x == 1 and y == 3):
		ga += 1
	elif y == x:
		pass
	else:
		gb += 1
	cnt += 1
	if cnt == k:
		print(ga, gb)
		sys.exit()
	a = alice[x - 1][y - 1]
	b = bob[x - 1][y - 1]
	if a == curx and b == cury:
		break
	else:
		vis[a, b] = 1
		q.append([a, b])
ansa = ga
ansb = gb
k -= cnt
n = len(games)
x = k // n
y = k % n
ansa += x * games[-1][0]
y -= 1
if y >= 0:
	ansa += games[y][0]
ansb += x * games[-1][1]
if y >= 0:
	ansb += games[y][1]
print(ansa, ansb)


def readints():
	return [int(item) for item in input().strip().split()]

def cmp(a, b):
	return {1: {1: (0, 0), 2: (0, 1), 3: (1, 0)}, 2: {1: (1, 0), 2: (0, 0), 3: (0, 1)}, 3: {1: (0, 1), 2: (1, 0), 3: (0, 0)}}[a][b]

class Solver:

	def main(self):
		(n, a, b) = readints()
		A = [0, [0] + readints(), [0] + readints(), [0] + readints()]
		B = [0, [0] + readints(), [0] + readints(), [0] + readints()]
		current = (a, b)
		results = [current]
		while True:
			current = (A[current[0]][current[1]], B[current[0]][current[1]])
			if current not in results:
				results.append(current)
			else:
				break
		first_cycle = results.index(current)
		totals = (0, 0)
		for i in range(first_cycle):
			res = cmp(*results[i])
			totals = (totals[0] + res[0], totals[1] + res[1])
			n -= 1
			if n == 0:
				break
		cycled = len(results) - first_cycle
		if n > 0:
			multi = n // cycled
			for i in range(first_cycle, len(results)):
				res = cmp(*results[i])
				totals = (totals[0] + multi * res[0], totals[1] + multi * res[1])
			for i in range(n % cycled):
				res = cmp(*results[i + first_cycle])
				totals = (totals[0] + res[0], totals[1] + res[1])
		print('{} {}'.format(*totals))
Solver().main()