import java.io.*
import java.util.*

fun <T : Comparable<T>> max(a: T, b: T): T = if (b > a) b else a
fun <T : Comparable<T>> min(a: T, b: T): T = if (b < a) b else a
fun Number.isEven(): Boolean = if (this.toInt() and 1 == 0) true else false
fun Number.isOdd(): Boolean = !this.isEven()

tailrec fun gcd(a: Int, b: Int): Int {
    if (a == 0) return b
    else return gcd(b % a, a)
}

fun main(args: Array<String>) {
    if (args.isNotEmpty()) System.setIn(FileInputStream(args[0]))
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))

    val aprime = 573259391
    val outstr = StringBuilder(10000)
    var st = StringTokenizer(br.readLine())
    val n = st.nextToken().toInt()
    st = StringTokenizer(br.readLine())
    var k = 0
    var oldai = st.nextToken().toInt()
    outstr.append(oldai)
    for (i in 2..n) {
        var newai = st.nextToken().toInt()
        if (gcd(oldai, newai) > 1) {
            ++k
            outstr.append(" "); outstr.append(aprime)
        }
        outstr.append(" "); outstr.append(newai)
        oldai = newai
    }
    bw.write("$k\n")
    bw.write(outstr.toString())
    bw.newLine()
    bw.flush()
}


import java.io.*
import java.util.*

fun <T : Comparable<T>> max(a: T, b: T): T = if (b > a) b else a
fun <T : Comparable<T>> min(a: T, b: T): T = if (b < a) b else a
fun Number.isEven(): Boolean = if (this.toInt() and 1 == 0) true else false
fun Number.isOdd(): Boolean = !this.isEven()

tailrec fun gcd(a: Int, b: Int): Int {
    if (a == 0) return b
    else return gcd(b % a, a)
}

fun main(args: Array<String>) {
    if (args.isNotEmpty()) System.setIn(FileInputStream(args[0]))
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))

    val aprime = 573259391
    val outstr = StringBuilder(10000)
    var st = StringTokenizer(br.readLine())
    val n = st.nextToken().toInt()
    st = StringTokenizer(br.readLine())
    var k = 0
    var oldai = st.nextToken().toInt()
    outstr.append(oldai)
    for (i in 2..n) {
        var newai = st.nextToken().toInt()
        if (gcd(oldai, newai) > 1) {
            ++k
            outstr.append(" "); outstr.append(aprime)
        }
        outstr.append(" "); outstr.append(newai)
        oldai = newai
    }
    bw.write(outstr.toString())
    bw.newLine()
    bw.flush()
}


#include <stdio.h>
typedef unsigned int uint;
typedef enum{false,true}boole;

boole check( uint a, uint b );

void main(){
    uint n,cont,i,z,au;
    boole r;
    char msg[100000];

    scanf("%d",&n);
    uint elms[n];
    uint elms2[n+n];
    cont=au=0;

    // SET TO 0
    memset( elms2,0,sizeof(elms2) );

    strcpy(msg,"");

    for(i=0;i<n;i++)
        scanf("%d",elms+i);

    for(i=0;i<n;i++){
        if( i<n-1 ){
            r = check( elms[i], elms[i+1] );
            if( !r ){
                cont++;
                elms2[au++]=elms[i];
                elms2[au++]=1;
            }else
                elms2[au++]=elms[i];
        }else
            elms2[au++]=elms[i];

    }
    printf("%d\n",cont);
    for(i=0;i<n+n && elms2[i] != 0;i++){
        printf("%d ",elms2[i]);
    }
    exit(0);
}

boole check( uint a, uint b ){
    while(a != 0 && b!=0){
        if( a > b )
            a%=b;
        else
            b%=a;
    }
    return ( ( a>b )? a:b )== 1 ? 1:0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <cmath>
#include <string>
#include <map>
#include <ctime>

using namespace std;

const int INF = 1e9 + 9;
const int MOD = 1e9 + 7;

int f(int a, int b) {
	if (a < b) {
		if (a == 1 & b == 3) {
			return 0;
		} else {
			return 1;
		}
	}
	if (a > b) {
		if (a == 3 & b == 1) {
			return 1;
		} else {
			return 0;
		}
	}
	if (a == b) {
		return -1;
	}
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif

	long long n;
	int a, b, used[4][4];
	scanf("%I64d%d%d", &n, &a, &b);
	vector<int> al[4], bb[4];
	pair<int, int> k[4][4];
	for (int i = 1; i <= 3; ++i) {
		al[i].push_back(0);
		for (int j = 0; j < 3; ++j) {
			int c;
			scanf("%d", &c);
			al[i].push_back(c);
			used[i][j + 1] = 0;
			k[i][j + 1] = make_pair(0, 0);
		}
	}
	for (int i = 1; i <= 3; ++i) {
		bb[i].push_back(0);
		for (int j = 1; j <= 3; ++j) {
			int c;
			scanf("%d", &c);
			bb[i].push_back(c);
		}
	}
	pair<long long, long long> ans = make_pair(0, 0);
	int a1 = a, b1 = b, ost = 0, pa = a, pb = b;
	for (int i = 1; i <= 10; ++i) {
		if (used[a1][b1] != 0) {
			ans = k[a1][b1];
			long long d = (n - used[a1][b1]) / (i - used[a1][b1]);
			ost = (n - used[a1][b1]) % (i - used[a1][b1]);
			long long pla = k[pa][pb].first - k[a1][b1].first, plb = (k[pa][pb].second - k[a1][b1].second);
			if (f(a1, b1) == 1) {
				plb++;
			}
			if (f(a1, b1) == 0) {
				pla++;
			}
			ans.first += pla * d;
			ans.second+= plb * d;
			break;
		}
		k[a1][b1] = k[pa][pb];
		pa = a1;
		pb = b1;
		used[a1][b1] = i;
		if (a1 < b1) {
			if (a1 == 1 && b1 == 3) {
				k[a1][b1].first++;
			} else {
				k[a1][b1].second++;
			}
		}
		if (a1 > b1) {
			if (a1 == 3 && b1 == 1) {
				k[a1][b1].second++;
			} else {
				k[a1][b1].first++;
			}
		}
		a1 = al[pa][pb];
		b1 = bb[pa][pb];
	}
	pa = a1;
	pb = b1;
	while (ost) {
		a1 = al[pa][pb];
		b1 = bb[pa][pb];
		if (a1 < b1) {
			if (a1 == 1 && b1 == 3) {
				ans.first++;
			} else {
				ans.second++;
			}
		}
		if (a1 > b1) {
			if (a1 == 3 && b1 == 1) {
				ans.second++;
			} else {
				ans.first++;
			}
		}
		pa = a1;
		pb = b1;
		ost--;
	}
	printf("%I64d %I64d\n", ans.first, ans.second);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <cmath>
#include <string>
#include <map>
#include <ctime>

using namespace std;

const int INF = 1e9 + 9;
const int MOD = 1e9 + 7;

int f(int a, int b) {
	if (a < b) {
		if (a == 1 & b == 3) {
			return 0;
		} else {
			return 1;
		}
	}
	if (a > b) {
		if (a == 3 & b == 1) {
			return 1;
		} else {
			return 0;
		}
	}
	if (a == b) {
		return -1;
	}
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif

	long long n;
	int a, b, used[4][4];
	scanf("%I64d%d%d", &n, &a, &b);
	vector<int> al[4], bb[4];
	pair<int, int> k[4][4];
	for (int i = 1; i <= 3; ++i) {
		al[i].push_back(0);
		for (int j = 0; j < 3; ++j) {
			int c;
			scanf("%d", &c);
			al[i].push_back(c);
			used[i][j + 1] = 0;
			k[i][j + 1] = make_pair(0, 0);
		}
	}
	for (int i = 1; i <= 3; ++i) {
		bb[i].push_back(0);
		for (int j = 1; j <= 3; ++j) {
			int c;
			scanf("%d", &c);
			bb[i].push_back(c);
		}
	}
	pair<long long, long long> ans = make_pair(0, 0);
	int a1 = a, b1 = b, ost = 0, pa = a, pb = b;
	bool ind = 0;
	for (int i = 1; i <= min(n, (long long) 10); ++i) {
		if (used[a1][b1] != 0) {
			ind = 1;
			ans = k[a1][b1];
			long long d = (n - used[a1][b1]) / (i - used[a1][b1]);
			ost = (n - used[a1][b1]) % (i - used[a1][b1]);
			long long pla = k[pa][pb].first - k[a1][b1].first, plb = (k[pa][pb].second - k[a1][b1].second);
			if (f(a1, b1) == 1) {
				plb++;
			}
			if (f(a1, b1) == 0) {
				pla++;
			}
			ans.first += pla * d;
			ans.second+= plb * d;
			break;
		}
		k[a1][b1] = k[pa][pb];
		pa = a1;
		pb = b1;
		used[a1][b1] = i;
		if (a1 < b1) {
			if (a1 == 1 && b1 == 3) {
				k[a1][b1].first++;
			} else {
				k[a1][b1].second++;
			}
		}
		if (a1 > b1) {
			if (a1 == 3 && b1 == 1) {
				k[a1][b1].second++;
			} else {
				k[a1][b1].first++;
			}
		}
		a1 = al[pa][pb];
		b1 = bb[pa][pb];
	}
	if (!ind) {
		ost = n;
		a1 = a;
		b1 = b;
	}
	pa = a1;
	pb = b1;
	while (ost) {
		a1 = al[pa][pb];
		b1 = bb[pa][pb];
		if (a1 < b1) {
			if (a1 == 1 && b1 == 3) {
				ans.first++;
			} else {
				ans.second++;
			}
		}
		if (a1 > b1) {
			if (a1 == 3 && b1 == 1) {
				ans.second++;
			} else {
				ans.first++;
			}
		}
		pa = a1;
		pb = b1;
		ost--;
	}
	printf("%I64d %I64d\n", ans.first, ans.second);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <cmath>
#include <string>
#include <map>
#include <ctime>

using namespace std;

const int INF = 1e9 + 9;
const int MOD = 1e9 + 7;

int f(int a, int b) {
	if (a < b) {
		if (a == 1 & b == 3) {
			return 0;
		} else {
			return 1;
		}
	}
	if (a > b) {
		if (a == 3 & b == 1) {
			return 1;
		} else {
			return 0;
		}
	}
	if (a == b) {
		return -1;
	}
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif

	long long n;
	int a, b, used[4][4];
	scanf("%I64d%d%d", &n, &a, &b);
	vector<int> al[4], bb[4];
	pair<int, int> k[4][4];
	for (int i = 1; i <= 3; ++i) {
		al[i].push_back(0);
		for (int j = 0; j < 3; ++j) {
			int c;
			scanf("%d", &c);
			al[i].push_back(c);
			used[i][j + 1] = 0;
			k[i][j + 1] = make_pair(0, 0);
		}
	}
	for (int i = 1; i <= 3; ++i) {
		bb[i].push_back(0);
		for (int j = 1; j <= 3; ++j) {
			int c;
			scanf("%d", &c);
			bb[i].push_back(c);
		}
	}
	pair<long long, long long> ans = make_pair(0, 0);
	int a1 = a, b1 = b, ost, pa = a, pb = b;
	for (int i = 1; i <= 9; ++i) {
		if (used[a1][b1] != 0) {
			ans = k[a1][b1];
			int d = (n - used[a1][b1]) / (i - used[a1][b1]);
			ost = (n - used[a1][b1]) % (i - used[a1][b1]);
			int pla = k[pa][pb].first - k[a1][b1].first, plb = (k[pa][pb].second - k[a1][b1].second);
			if (f(a1, b1) == 1) {
				plb++;
			}
			if (f(a1, b1) == 0) {
				pla++;
			}
			ans.first += pla * d;
			ans.second+= plb * d;
			break;
		}
		k[a1][b1] = k[pa][pb];
		pa = a1;
		pb = b1;
		used[a1][b1] = i;
		if (a1 < b1) {
			if (a1 == 1 && b1 == 3) {
				k[a1][b1].first++;
			} else {
				k[a1][b1].second++;
			}
		}
		if (a1 > b1) {
			if (a1 == 3 && b1 == 1) {
				k[a1][b1].second++;
			} else {
				k[a1][b1].first++;
			}
		}
		a1 = al[pa][pb];
		b1 = bb[pa][pb];
	}
	while (ost) {
		int ta = a1, tb = b1;
		if (a1 < b1) {
			if (a1 == 1 && b1 == 3) {
				ans.first++;
			} else {
				ans.second++;
			}
		}
		if (a1 > b1) {
			if (a1 == 1 && b1 == 3) {
				ans.second++;
			} else {
				ans.first++;
			}
		}
		a1 = al[ta][tb];
		b1 = bb[ta][tb];
		ost--;
	}
	printf("%I64d %I64d\n", ans.first, ans.second);
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;

class X {
public :
	int index;
	int winA;
	int winB;
	bool pExit;
};

int main() {
	vector <vector <int>> B(3),A(3);
	vector <X> xp0;
	for (int i = 0; i < 3; i++) {
		B[i].resize(3);
		A[i].resize(3);
	}
	long long k,winA = 0,winB = 0,wA = 0, wB = 0;
	int a, b, p0 = 0, T;
	cin >> k >> a >> b; a--; b--;
	for(int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> A[i][q];
			A[i][q]--;
		}
	for (int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> B[i][q];
			B[i][q]--;
		}

	set <int> m;//храним пройденные вершины
	int u;//точка входа
	//находим цикл и несколько элементов вне цикла
	for (int i = 0; i < 10; i++) {
		int tmpa = a, tmpb = b;
		X tmpx;
		tmpx.pExit = false; tmpx.winA = 0; tmpx.winB = 0;
		tmpx.index = a * 10 + b;
		if (a == 1 && b == 0 || a == 2 && b == 1 || a == 0 && b == 2)
			tmpx.winA = 1;
		if (b == 1 && a == 0 || b == 2 && a == 1 || b == 0 && a == 2)
			tmpx.winB = 1;
		if (m.find(tmpx.index) != m.end()) {
			for(u = 0; u < xp0.size(); u++)
				if (xp0[u].index == tmpx.index) {
					xp0[u].pExit = true;
					T = p0 - u;
					break;
				}
			break;
		}
		a = A[tmpa][tmpb];
		b = B[tmpa][tmpb];
		p0++;
		m.insert(tmpx.index);
		xp0.push_back(tmpx);
	}

	for (int i = 0; i < p0 - T; i++) {
		if (i >= k) {
			cout << winA << " " << winB;
			return 0;
		}
		winA += xp0[i].winA;
		winB += xp0[i].winB;
	}
	//сумма побед А и В за цикл
	for (int i = u; i < u + T; i++) {
		wA += xp0[i].winA;
		wB += xp0[i].winB;
	}

	//сумма побед А и В за все циклы
	for (int i = 0; i < (k - u) / T; i++) {
		winA += wA;
		winB += wB;
	}

	//сумма побед в не полном цикле
	for (int i = u; i < u + (k - u) % T; i++) {
		winA += xp0[i].winA;
		winB += xp0[i].winB;
	}

	cout << winA << " " << winB << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;

class X {
public :
	int index;
	int winA;
	int winB;
};

int main() {
	vector <vector <int>> B(3),A(3);
	vector <X> xp0;
	for (int i = 0; i < 3; i++) {
		B[i].resize(3);
		A[i].resize(3);
	}
	long long k,winA = 0,winB = 0;
	int a, b, p0 = 0, T;
	cin >> k >> a >> b; a--; b--;
	for(int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> A[i][q];
			A[i][q]--;
		}
	for (int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> B[i][q];
			B[i][q]--;
		}

	set <int> m;//храним пройденные вершины
	int u;//точка входа
	//находим цикл и несколько элементов вне цикла
	for (int i = 0; i < 10; i++) {
		int tmpa = a, tmpb = b;
		X tmpx;
		tmpx.winA = 0; tmpx.winB = 0;
		tmpx.index = a * 10 + b;
		if (a == 1 && b == 0 || a == 2 && b == 1 || a == 0 && b == 2)
			tmpx.winA = 1;
		if (b == 1 && a == 0 || b == 2 && a == 1 || b == 0 && a == 2)
			tmpx.winB = 1;
		if (m.find(tmpx.index) != m.end()) {
			for(u = 0; u < xp0.size(); u++)
				if (xp0[u].index == tmpx.index) {
					T = xp0.size() - u;
					break;
				}
			break;
		}
		a = A[tmpa][tmpb];
		b = B[tmpa][tmpb];
		m.insert(tmpx.index);
		xp0.push_back(tmpx);
	}
	//сумма побед за отрезок
	for (int i = 0; i < xp0.size() - T; i++) {
		if (i >= k) {
			cout << winA << " " << winB;
			return 0;
		}
		winA += xp0[i].winA;
		winB += xp0[i].winB;
	}
	//сумма побед А и В за все циклы
	for (int i = u; i < u + T; i++) {
		winA += xp0[i].winA*(k - u) / T;
		winB += xp0[i].winA*(k - u) / T;
	}

	//сумма побед в не полном цикле
	for (int i = u; i < u + (k - u) % T; i++) {
		winA += xp0[i].winA;
		winB += xp0[i].winB;
	}

	cout << winA << " " << winB << endl;
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int B[3][3], A[3][3];
	long long k, winA = 0, winB = 0;
	int a, b;
	cin >> k >> a >> b;
	a--; b--;

	for(int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> A[i][q];
			A[i][q]--;
		}
	for (int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> B[i][q];
			B[i][q]--;
		}

	for (int i = 0; i < k; i++) {
		int tmpa = a, tmpb = b;
		if (a + 1 == b)
			winB++;
		else
			if (b + 1 == a)
				winA++;
		a = A[tmpa][tmpb];
		b = B[tmpa][tmpb];
	}
	cout << winA << " " << winB;
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;

class X {
public:
	int index;
	int winA;
	int winB;
};

int B[4][4], A[4][4];
vector <X> game;
long long k, winA = 0, winB = 0;
int a, b, T = 0, u = 0;
set <int> m;

void Input() {
	cin >> k >> a >> b;

	for (int i = 1; i < 4; i++)
		for (int q = 1; q < 4; q++)
			cin >> A[i][q];
	for (int i = 1; i < 4; i++)
		for (int q = 1; q < 4; q++)
			cin >> B[i][q];
}

void Output() {
	cout << winA << " " << winB << endl;
	exit(0);
}

void findCycle() {
	for (int i = 0; i < 10; i++) {
		int tmpa = a, tmpb = b;
		X tmpx;
		tmpx.winA = 0; tmpx.winB = 0;
		tmpx.index = a * 10 + b;
		if (a == 2 && b == 1 || a == 3 && b == 2 || a == 1 && b == 3)
			tmpx.winA = 1;
		if (b == 2 && a == 1 || b == 3 && a == 2 || b == 1 && a == 3)
			tmpx.winB = 1;
		if (m.find(tmpx.index) != m.end()) {
			for (; u < game.size(); u++)
				if (game[u].index == tmpx.index) {
					T = game.size() - u;
					break;
				}
			break;
		}
		a = A[tmpa][tmpb];
		b = B[tmpa][tmpb];
		m.insert(tmpx.index);
		game.push_back(tmpx);
	}
}

void Body() {
	//находим цикл и несколько элементов вне цикла
	findCycle();

	for (int i = 0; i < game.size() - T; i++) {
		if (i >= k)
			Output();
		winA += game[i].winA;
		winB += game[i].winB;
	}

	//сумма побед А и В за все циклы
	for (int i = u; i < u + T; i++) {
		winA += game[i].winA * (k - u) / T;
		winB += game[i].winB * (k - u) / T;
	}

	//сумма побед в не полном цикле
	for (int i = u; i < u + (k - u) % T; i++) {
		winA += game[i].winA;
		winB += game[i].winB;
	}

}

int main() {
	Input();
	Body();
	Output();
}


#include <iostream>
using namespace std;

int main() {
	int B[3][3], A[3][3];
	long long k, winA = 0, winB = 0;
	int a, b;
	cin >> k >> a >> b;
	a--; b--;

	for(int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> A[i][q];
			A[i][q]--;
		}
	for (int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> B[i][q];
			B[i][q]--;
		}

	for (int i = 0; i < k; i++) {
		int tmpa = a, tmpb = b;
		if (a + 1 == b || a == 2 && b == 0)
			winB++;
		else
			if (b + 1 == a || b == 2 && a == 0)
				winA++;
		a = A[tmpa][tmpb];
		b = B[tmpa][tmpb];
	}
	cout << winA << " " << winB;
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;

class X {
public :
	int index;
	int winA;
	int winB;
};

int main() {
	vector <vector <int>> B(3),A(3);
	vector <X> xp0;
	for (int i = 0; i < 3; i++) {
		B[i].resize(3);
		A[i].resize(3);
	}
	long long k,winA = 0,winB = 0;
	int a, b, T;
	cin >> k >> a >> b; a--; b--;
	for(int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> A[i][q];
			A[i][q]--;
		}
	for (int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> B[i][q];
			B[i][q]--;
		}

	set <int> m;//храним пройденные вершины
	int u;//точка входа
	//находим цикл и несколько элементов вне цикла
	for (int i = 0; i < 10; i++) {
		int tmpa = a, tmpb = b;
		X tmpx;
		tmpx.winA = 0; tmpx.winB = 0;
		tmpx.index = a * 10 + b;
		if (a == 1 && b == 0 || a == 2 && b == 1 || a == 0 && b == 2)
			tmpx.winA = 1;
		if (b == 1 && a == 0 || b == 2 && a == 1 || b == 0 && a == 2)
			tmpx.winB = 1;
		if (m.find(tmpx.index) != m.end()) {
			for(u = 0; u < xp0.size(); u++)
				if (xp0[u].index == tmpx.index) {
					T = xp0.size() - u;
					break;
				}
			break;
		}
		a = A[tmpa][tmpb];
		b = B[tmpa][tmpb];
		m.insert(tmpx.index);
		xp0.push_back(tmpx);
	}
	//сумма побед за отрезок
	for (int i = 0; i < xp0.size() - T; i++) {
		if (i >= k) {
			cout << winA << " " << winB;
			return 0;
		}
		winA += xp0[i].winA;
		winB += xp0[i].winB;
	}
	//сумма побед А и В за все циклы
	for (int i = u; i < u + T; i++) {
		winA += xp0[i].winA * ((k - u) / T);
		winB += xp0[i].winB * ((k - u) / T);
	}

	//сумма побед в не полном цикле
	for (int i = u; i < u + (k - u) % T; i++) {
		winA += xp0[i].winA;
		winB += xp0[i].winB;
	}

	cout << winA << " " << winB << endl;
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	vector <vector <int>> B(3),A(3);
	for (int i = 0; i < 3; i++) {
		B[i].resize(3);
		A[i].resize(3);
	}
	long long k,winA = 0,winB = 0;
	int a, b;
	cin >> k >> a >> b; a--; b--;
	for(int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> A[i][q];
			A[i][q]--;
		}
	for (int i = 0; i < 3; i++)
		for (int q = 0; q < 3; q++) {
			cin >> B[i][q];
			B[i][q]--;
		}

	for (int i = 0; i < k; i++) {
		int tmpa = a, tmpb = b;
		if (a == 0 && b == 1 || a == 1 && b == 2 || a == 2 && b == 0)
			winB++;
		if (b == 0 && a == 1 || b == 1 && a == 2 || b == 2 && a == 0)
			winA++;
		a = A[tmpa][tmpb];
		b = B[tmpa][tmpb];
	}
	cout << winA << " " << winB;
	return 0;
}


#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<utility>
#include<map>
#include <unordered_map>
#include <bitset>
using namespace std;
typedef long long ll;
ll K, X, Y, xx[100], yy[100], V[4][4]; int A[4][4], B[4][4];
void inline g(int aa, int bb) {
	if (aa == 1) {
		if (bb == 2) Y++;
		else if (bb == 3) X++;
	}
	else if (aa == 2) {
		if (bb == 1) X++;
		else if (bb == 3) Y++;
	}
	else {
		if (bb == 1) Y++;
		else if (bb == 2) X++;
	}
}
void f(int aa, int bb, ll kk) {
	if (kk > K) return;
	if (!V[aa][bb]) {
		V[aa][bb]=kk;
		g(aa, bb);
		xx[kk] = X; yy[kk] = Y;
		f(A[aa][bb], B[aa][bb], kk + 1);
	}
	else if (V[aa][bb] == -1) {
		g(aa, bb);
		f(A[aa][bb], B[aa][bb], kk + 1);
	}
	else {
		ll d, p, q;
		g(aa, bb);
		xx[kk] = X; yy[kk] = Y;
		d = kk - V[aa][bb];
		p = K - kk;
		q = p / d;
		X += (X - xx[V[aa][bb]])*q;
		Y += (Y - yy[V[aa][bb]])*q;
		for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) V[i][j] = -1;
		f(A[aa][bb], B[aa][bb], kk + q * d + 1);
	}
}
int main() {
	int i, j, a, b;
	cin >> K >> a >> b;
	for (i = 1; i <= 3; i++) for (j = 1; j <= 3; j++) cin >> A[i][j];
	for (i = 1; i <= 3; i++) for (j = 1; j <= 3; j++) cin >> B[i][j];
	f(a, b, 1);
	cout << X << " " << Y;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <cmath>

using namespace std;

typedef long long ll;
const ll K = 1;

#define mp make_pair
#define pb push_back
#define pdd pair <double, double>
#define pii pair <int, int>
#define pll pair <ll, ll>

#define MOD 1000000007
#define INF 1000000000
#define M_PI 3.14159265358979323846

ll A[4][4], B[4][4];
vector <ll> ansa, ansb;
map <pll, ll> M;

ll f(ll x, ll y)
{
	if (x == 1 && y == 3) return 1;
	if (x == 2 && y == 1) return 1;
	if (x == 3 && y == 2) return 1;
	return 0;
}

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

	ll k, af, bf;
	ll a, b, i, j, a1, b1;
	cin >> k >> a >> b;
	for (i = 1; i < 4; ++i)
	{
		for (j = 1; j < 4; ++j)
		{
			cin >> A[i][j];
		}
	}
	for (i = 1; i < 4; ++i)
	{
		for (j = 1; j < 4; ++j)
		{
			cin >> B[i][j];
		}
	}
	ansa.push_back(0);
	ansb.push_back(0);
	ansa.push_back(f(a, b));
	ansb.push_back(f(b, a));
	M[mp(a, b)] = 1;
	i = 1;
	while (1)
	{
		i++;
		a1 = A[a][b];
		b1 = B[a][b];
		if (M.count(mp(a1, b1)))
		{
			j = M[mp(a1, b1)];
			break;
		}
		M[mp(a1, b1)] = i;
		ansa.push_back(ansa[i - 1] + f(a1, b1));
		ansb.push_back(ansb[i - 1] + f(b1, a1));
		a = a1;
		b = b1;
	}
	if (k < i)
	{
		cout << ansa[k] << ' ' << ansb[k];
		return 0;
	}
	af = ansa[j - 1];
	bf = ansb[j - 1];
	k -= j - 1;
	af += k / (i - j) * (ansa[i - 1] - ansa[j - 1]);
	bf += k / (i - j) * (ansb[i - 1] - ansb[j - 1]);
	k -= k / (i - j) * (i - j);
	af += ansa[j - 1 + k] - ansa[j - 1];
	bf += ansb[j - 1 + k] - ansb[j - 1];

	cout << af << ' ' << bf;

    return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <cmath>

using namespace std;

typedef long long ll;
const ll K = 1;

#define mp make_pair
#define pb push_back
#define pdd pair <double, double>
#define pii pair <int, int>
#define pll pair <ll, ll>

#define MOD 1000000007
#define INF 1000000000
#define M_PI 3.14159265358979323846

ll A[4][4], B[4][4];
vector <ll> ansa, ansb;
map <pll, ll> M;

ll f(ll x, ll y)
{
	if (x == 1 && y == 3) return 1;
	if (x == 2 && y == 1) return 1;
	if (x == 3 && y == 2) return 1;
	return 0;
}

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

	ll k, af, bf;
	ll a, b, i, j, a1, b1;
	cin >> k >> a >> b;
	for (i = 1; i < 4; ++i)
	{
		for (j = 1; j < 4; ++j)
		{
			cin >> A[i][j];
		}
	}
	for (i = 1; i < 4; ++i)
	{
		for (j = 1; j < 4; ++j)
		{
			cin >> B[i][j];
		}
	}
	ansa.push_back(0);
	ansb.push_back(0);
	ansa.push_back(f(a, b));
	ansb.push_back(f(b, a));
	M[mp(a, b)] = 1;
	i = 1;
	while (1)
	{
		i++;
		a1 = A[a][b];
		b1 = B[a][b];
		if (M.count(mp(a1, b1)))
		{
			j = M[mp(a1, b1)];
			break;
		}
		if (i == 10) break;
		M[mp(a1, b1)] = i;
		ansa.push_back(ansa[i - 1] + f(a1, b1));
		ansb.push_back(ansb[i - 1] + f(b1, a1));
		a = a1;
		b = b1;
	}
	if (k <= i)
	{
		cout << ansa[k] << ' ' << ansb[k];
		return 0;
	}
	af = ansa[j - 1];
	bf = ansb[j - 1];
	k -= j - 1;
	af += k / (i - j) * (ansa[i - 1] - ansa[j - 1]);
	bf += k / (i - j) * (ansb[i - 1] - ansb[j - 1]);
	k -= k / (i - j) * (i - j);
	af += ansa[j - 1 + k] - ansa[j - 1];
	bf += ansb[j - 1 + k] - ansb[j - 1];

	cout << af << ' ' << bf;

    return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <cmath>

using namespace std;

typedef long long ll;
const ll K = 1;

#define mp make_pair
#define pb push_back
#define pdd pair <double, double>
#define pii pair <int, int>
#define pll pair <ll, ll>

#define MOD 1000000007
#define INF 1000000000
#define M_PI 3.14159265358979323846

int A[4][4], B[4][4];
vector <int> ansa, ansb;
map <pii, int> M;

int f(int x, int y)
{
	if (x == 1 && y == 3) return 1;
	if (x == 2 && y == 1) return 1;
	if (x == 3 && y == 2) return 1;
	return 0;
}

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

	ll k, af, bf;
	int a, b, i, j, a1, b1;
	cin >> k >> a >> b;
	for (i = 1; i < 4; ++i)
	{
		for (j = 1; j < 4; ++j)
		{
			cin >> A[i][j];
		}
	}
	for (i = 1; i < 4; ++i)
	{
		for (j = 1; j < 4; ++j)
		{
			cin >> B[i][j];
		}
	}
	ansa.push_back(0);
	ansb.push_back(0);
	ansa.push_back(f(a, b));
	ansb.push_back(f(b, a));
	M[mp(a, b)] = 1;
	i = 1;
	while (1)
	{
		i++;
		a1 = A[a][b];
		b1 = B[a][b];
		if (M.count(mp(a1, b1)))
		{
			j = M[mp(a1, b1)];
			break;
		}
		if (i == 10) break;
		M[mp(a1, b1)] = i;
		ansa.push_back(ansa[i - 1] + f(a1, b1));
		ansb.push_back(ansb[i - 1] + f(b1, a1));
		a = a1;
		b = b1;
	}
	if (k <= i)
	{
		cout << ansa[k] << ' ' << ansb[k];
		return 0;
	}
	af = ansa[j - 1];
	bf = ansb[j - 1];
	k -= j - 1;
	af += k / (i - j) * (ansa[i - 1] - ansa[j - 1]);
	bf += k / (i - j) * (ansb[i - 1] - ansb[j - 1]);
	k -= k / (i - j) * (i - j);
	af += ansa[j - 1 + k] - ansa[j - 1];
	bf += ansb[j - 1 + k] - ansb[j - 1];

	cout << af << ' ' << bf;

    return 0;
}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <deque>
#include <algorithm>
#include <fstream>
#include <time.h>
#include <string>

#define ll long long
#define ull unsigned long long
#define base 1000000007
#define kN 20//8020

using namespace std;

class Qayl
{
public:
    bool exela;
    long long sA;
    long long sB;
    long long qayl;

    Qayl()
    {
        exela = false;
    }
};

long long Result(long long a, long long b)
{
    if(a==b)return 0;
    if((a==3 && b==2) || (a==2 && b==1) || (a==1 && b==3))return 1;
    return -1;
}

int main()
{
    clock_t tStart = clock();

    /*std::ofstream out("/Users/Sako/Работа/CodeForces/CodeForces/in.txt");
     std::cout.rdbuf(out.rdbuf());

     cout<<30<<endl;
     for(int i=0;i<30;i++)
     {
     cout<<i+1<<" ";
     }
     return 0;*/

#ifdef DEBUG
    std::ifstream in("/Users/Sako/Работа/CodeForces/CodeForces/in.txt");
    std::cin.rdbuf(in.rdbuf());
#endif

    long long k, a, b;
    cin>>k>>a>>b;
    vector<vector<long long>> ali(4, vector<long long>(4));
    vector<vector<long long>> bob(4, vector<long long>(4));

    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
            cin>>ali[i][j];
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
            cin>>bob[i][j];

    vector<vector<Qayl>> cikl(4, vector<Qayl>(4));
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
            cikl[i][j] = *new Qayl();

    long long sA = 0, sB = 0;
    long long res = Result(a, b);
    if(res==1)sA++;
    if(res==-1)sB++;
    cikl[a][b].exela = true;
    cikl[a][b].qayl = 1;
    cikl[a][b].sA = sA;
    cikl[a][b].sB = sB;

    long long i=2;
    for(;i<=k;i++)
    {
        long long norA = ali[a][b];
        long long norB = bob[a][b];
        a = norA;
        b = norB;

        res = Result(a, b);
        if(res==1)sA++;
        if(res==-1)sB++;

        if(cikl[a][b].exela)
        {
            long long coef = (k-cikl[a][b].qayl)/(i-cikl[a][b].qayl);
            sA+=(coef-1)*(sA-cikl[a][b].sA);
            sB+=(coef-1)*(sB-cikl[a][b].sB);
            i+=coef*(i-cikl[a][b].qayl);
            break;
        }

        cikl[a][b].exela = true;
        cikl[a][b].qayl = i;
        cikl[a][b].sA = sA;
        cikl[a][b].sB = sB;
    }

    for(;i<=k;i++)
    {
        long long norA = ali[a][b];
        long long norB = bob[a][b];
        a = norA;
        b = norB;

        res = Result(a, b);
        if(res==1)sA++;
        if(res==-1)sB++;
    }

    cout<<sA<<" "<<sB;

#ifdef DEBUG
    cout<<endl;
    printf("Time taken: %.3fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#endif
    return 0;
}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <deque>
#include <algorithm>
#include <fstream>
#include <time.h>
#include <string>

#define ll long long
#define ull unsigned long long
#define base 1000000007
#define kN 20//8020

using namespace std;

class Qayl
{
public:
    bool exela;
    long long sA;
    long long sB;
    long long qayl;

    Qayl()
    {
        exela = false;
    }
};

long long Result(long long a, long long b)
{
    if(a==b)return 0;
    if((a==3 && b==2) || (a==2 && b==1) || (a==1 && b==3))return 1;
    return -1;
}

int main()
{
    clock_t tStart = clock();

    /*std::ofstream out("/Users/Sako/Работа/CodeForces/CodeForces/in.txt");
     std::cout.rdbuf(out.rdbuf());

     cout<<30<<endl;
     for(int i=0;i<30;i++)
     {
     cout<<i+1<<" ";
     }
     return 0;*/

#ifdef DEBUG
    std::ifstream in("/Users/Sako/Работа/CodeForces/CodeForces/in.txt");
    std::cin.rdbuf(in.rdbuf());
#endif

    long long k, a, b;
    cin>>k>>a>>b;
    vector<vector<long long>> ali(4, vector<long long>(4));
    vector<vector<long long>> bob(4, vector<long long>(4));

    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
            cin>>ali[i][j];
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
            cin>>bob[i][j];

    vector<vector<Qayl>> cikl(4, vector<Qayl>(4));
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
            cikl[i][j] = *new Qayl();

    long long sA = 0, sB = 0;
    long long res = Result(a, b);
    if(res==1)sA++;
    if(res==-1)sB++;
    cikl[a][b].exela = true;
    cikl[a][b].qayl = 1;
    cikl[a][b].sA = sA;
    cikl[a][b].sB = sB;

    long long i=2;
    for(;i<=k;i++)
    {
        long long norA = ali[a][b];
        long long norB = bob[a][b];
        a = norA;
        b = norB;

        res = Result(a, b);
        if(res==1)sA++;
        if(res==-1)sB++;

        if(cikl[a][b].exela)
        {
            long long coef = (k-cikl[a][b].qayl)/(i-cikl[a][b].qayl);
            sA+=(coef-1)*(sA-cikl[a][b].sA);
            sB+=(coef-1)*(sB-cikl[a][b].sB);
            i+=(coef-1)*(i-cikl[a][b].qayl);
            i++;
            break;
        }

        cikl[a][b].exela = true;
        cikl[a][b].qayl = i;
        cikl[a][b].sA = sA;
        cikl[a][b].sB = sB;
    }

    for(;i<=k;i++)
    {
        long long norA = ali[a][b];
        long long norB = bob[a][b];
        a = norA;
        b = norB;

        res = Result(a, b);
        if(res==1)sA++;
        if(res==-1)sB++;
    }

    cout<<sA<<" "<<sB;

#ifdef DEBUG
    cout<<endl;
    printf("Time taken: %.3fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#endif
    return 0;
}


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):
	#	print(arr[q[j-1]][p[j-1]],q[i])
		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
#print(r,d)
for i in range(min(ans,k)):
	if w[i]==0:
		ze+=1
	if w[i]==1:
		on+=1
i=ans
#print(p)
#print(q)
while(r):
	if w[i]==0:
		ze+=1
	if w[i]==1:
		on+=1
	i+=1
	r-=1
print(ze,on)


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):
	#	print(arr[q[j-1]][p[j-1]],q[i])
		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)
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
#print(p)
#print(q)
while(r):
	if w[i]==0:
		ze+=1
	if w[i]==1:
		on+=1
	i+=1
	r-=1
print(ze,on)


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):
	#	print(arr[q[j-1]][p[j-1]],q[i])
		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)
ze=ze*d
on=on*d
for i in range(ans):
	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 = [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:q]
end = (k - len(sh)) % period
repeat = int((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)

# print(ah)
# print(bh)
# print(sh)
# print(period)
# print((q,r))
# print(score_pp)
# print(repeat)
# print((aadd,badd))
# print(end)

# Time Limit Exceeded #


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 = (k - len(sh) - end) / period
repeat = int(repeat)
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)

# print(ah)
# print(bh)
# print(sh)
# print((r,q))
# print(score_pp)
# print(period)
# print(end)
# print(repeat)
# print((aadd,badd))

# Time Limit Exceeded #


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 = (k - len(sh) - end) / period
repeat = int(repeat)
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)

print(ah)
print(bh)
print(sh)
print((r,q))
print(score_pp)
print(period)
print(end)
print(repeat)
print((aadd,badd))

# Time Limit Exceeded #


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)

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:q]
end = (k - len(sh)) % period
repeat = int((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)

# print(ah)
# print(bh)
# print(sh)
# print(period)
# print((q,r))
# print(score_pp)
# print(repeat)
# print((aadd,badd))
# print(end)

# Time Limit Exceeded #


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 = int((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)

# print(ah)
# print(bh)
# print(sh)
# print(period)
# print((q,r))
# print(score_pp)
# print(repeat)
# print((aadd,badd))
# print(end)

# Time Limit Exceeded #


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 = (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(int(ap), end = ' ')
print(int(bp))

# print(ah)
# print(bh)
# print(sh)
# print((r,q))
# print(score_pp)
# print(period)
# print(end)
# print(repeat)
# print((aadd,badd))

# Time Limit Exceeded #


from decimal import *
#print(getcontext())
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)

# print(ah)
# print(bh)
# print(sh)
# print((r,q))
# print(score_pp)
# print(period)
# print(end)
# print(repeat)
# print((aadd,badd))

# Time Limit Exceeded #


k, a, b = [int(i) for i in input().split(" ")]
A = []
B = []
ap = 0
bp = 0
ac = a
bc = b
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
if bc == ac + 1 or bc == ac - 2:
    bp += 1

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
    if bc == ac + 1 or bc == ac - 2:
        bp += 1

print(ap, end = ' ')
print(bp)


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()


# import collections

# n = int(raw_input())
# cnt = collections.Counter(map(int , raw_input().split()))
# odds = []
# for k in cnt.keys():
#     if cnt[k] % 2 == 1:
#         odds.append(k)

# minCost = 10**9

# for i in range(len(odds)):
#     for j in range(i + 1, len(odds)):
#         newLi = list(odds)
#         newLi.pop(i)
#         newLi.pop(j - 1)
#         newLi.sort()
#         # print newLi
#         cost = 0
#         for k in range(1 , len(newLi) , 2):
#             cost += newLi[k] - newLi[k - 1]

#         minCost = min(minCost , cost)

# print minCost

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:
        try:
            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
            x, y = a-1, b-1
            a, b = A[x][y], B[x][y]
            flag = 1
        except:
            print('in if')
    else:
        try:
            m[(a, b)] = cur
            ans1[cur], ans2[cur] = ans1[cur-1], ans2[cur-1]
        except:
            xyz = 0
            while True:
                xyz += 1

        if a == b%3 + 1:
            ac += 1
            ans1[cur] += 1
        if b == a%3 + 1:
            bc += 1
            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())
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
		x, y = a-1, b-1
		a, b = A[x][y], B[x][y]
		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())
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
		# x, y = a-1, b-1
		# a, b = A[x][y], B[x][y]
		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())
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
		x, y = a-1, b-1
		a, b = A[x][y], B[x][y]
		flag = 1
	else:
		m[(a, b)] = cur
		ans1[cur], ans2[cur] = ans1[cur-1], ans2[cur-1]
		if a == b%3 + 1:
			ac += 1
			ans1[cur] += 1
		if b == a%3 + 1:
			bc += 1
			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())
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 not flag:
		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
		x, y = a-1, b-1
		a, b = A[x][y], B[x][y]
		flag = 1
	else:
		m[(a, b)] = cur
		if cur <= 50:
			ans1[cur], ans2[cur] = ans1[cur-1], ans2[cur-1]
		if a == b%3 + 1:
			ac += 1
			if cur <= 50:
				ans1[cur] += 1
		if b == a%3 + 1:
			bc += 1
			if cur <= 50:
				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())
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 not flag:
		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
		x, y = a-1, b-1
		a, b = A[x][y], B[x][y]
		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)


# import collections

# n = int(raw_input())
# cnt = collections.Counter(map(int , raw_input().split()))
# odds = []
# for k in cnt.keys():
#     if cnt[k] % 2 == 1:
#         odds.append(k)

# minCost = 10**9

# for i in range(len(odds)):
#     for j in range(i + 1, len(odds)):
#         newLi = list(odds)
#         newLi.pop(i)
#         newLi.pop(j - 1)
#         newLi.sort()
#         # print newLi
#         cost = 0
#         for k in range(1 , len(newLi) , 2):
#             cost += newLi[k] - newLi[k - 1]

#         minCost = min(minCost , cost)

# print minCost

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:
        try:
            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
            x, y = a-1, b-1
            a, b = A[x][y], B[x][y]
            flag = 1
        except:
            print('in if')
    else:
        try:
            m[(a, b)] = cur
            ans1[cur], ans2[cur] = ans1[cur-1], ans2[cur-1]
            if a == b%3 + 1:
                ac += 1
                ans1[cur] += 1
            if b == a%3 + 1:
                bc += 1
                ans2[cur] += 1
            x, y = a-1, b-1
            a, b = A[x][y], B[x][y]
        except:
            xyz = 0
            while True:
                xyz += 1

    cur += 1

print(ac, bc)


completed={}

alice=[[0,-1,1],[1,0,-1],[-1,1,0]]
bob=[[0,1,-1],[-1,0,1],[1,-1,0]]

def simulate(i):

	global 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)


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 = {} # (a, b): (cyclelen, Alicegain, Bobgain)

    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)

    #print("Map", map123, "\nCycle", cycle123)

    ansA, ansB = 0, 0
    while k>0:
        if (a, b) in cycle123 and 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
        else:
            k -= 1
            ansA += int(point123[a, b] == "Alice")
            ansB += int(point123[a, b] == "Bob")
            a, b = map123[a, b]

ansa, ansb = solve()
print(ansa, ansb)


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 = {} # (a, b): (cyclelen, Alicegain, Bobgain)

    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)

    #print("Map", map123, "\nCycle", cycle123)

    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]

ansa, ansb = solve()
print(ansa, ansb)


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 = {} # (a, b): (nexta, nextb)
    cycle123 = {} # (a, b): (cyclelen, Alicegain, Bobgain)

    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)

    #print("Map", map123, "\nCycle", cycle123)

    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)


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 = {} # (a, b): (nexta, nextb)
    cycle123 = {} # (a, b): (cyclelen, Alicegain, Bobgain)

    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)

    print("Map", map123, "\nCycle", cycle123)

    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)


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 = {} # (a, b): (nexta, nextb)
    cycle123 = {} # (a, b): (cyclelen, Alicegain, Bobgain)

    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 < 100 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)

    #print("Map", map123, "\nCycle", cycle123)

    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)]

ansa, ansb = solve()
print(ansa, ansb)


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 = {} # (a, b): (nexta, nextb)
    cycle123 = {} # (a, b): (cyclelen, Alicegain, Bobgain)

    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)

    #print("Map", map123, "\nCycle", cycle123)

    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)]

ansa, ansb = solve()
print(ansa, ansb)


#!/usr/bin/env python3
# encoding: utf-8

#----------
# Constants
#----------

#----------
# Functions
#----------

# Reads a string from stdin, splits it by space chars, converts each
# substring to int, adds it to a list and returns the list as a result.
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)

#----------
# Execution start point
#----------

if __name__ == "__main__":
    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)

    # periods
    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

    # after
    rem = (k - index) % c
    for i in range(rem):
        da, db = results[i + index]
        apoint += da
        bpoint += db

    # before
    for res in results[:index]:
        da, db = res
        apoint += da
        bpoint += db

    print(apoint, bpoint)


# https://codeforces.com/problemset/problem/863/C
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))

# k = f0 + f*x + r
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)

#5 1 1
#1 2 2
#2 2 2
#2 2 2
#1 2 2
#2 2 2
#2 2 2


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]
if winner=='a':
	first_a_point=1
elif winner=='b':
	first_b_point=1
results=[]
is_found_loop=False
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]
	if winner=='a':
		a_point+=1
	elif winner=='b':
		b_point+=1
	result=str(a)+str(b)+winner
	if result in results:
		is_found_loop=True
		break
	results.append(result)
if is_found_loop:
	len_results=len(results)
	div=(k-1)//len_results
	a_point=b_point=0
	for i in range(len_results):
		winner=results[i][-1]
		if winner=='a':
			a_point+=1
		elif winner=='b':
			b_point+=1
	a_point*=div
	b_point*=div
	mod=(k-1)%len_results
	for i in range(mod):
		winner=results[i][-1]
		if winner=='a':
			a_point+=1
		elif winner=='b':
			b_point+=1
print(a_point+first_a_point,b_point+first_b_point)


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)


R=lambda: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)


R=lambda: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]
    c,d=divmod(k-i,i-j)
    z=zip(s[i],s[j],s[j+d])
    print(*map(lambda x:(c+1)*(x[0]-x[1])+x[2],z))


R=lambda: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]
    sr=s[cb+(k-i)%c]
    sa+=m*(sa-s[cb][0])+sr[0]-s[cb][0]
    sb+=m*(sb-s[cb][1])+sr[1]-s[cb][1]
    print(sa,sb)


R=lambda: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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


R=lambda: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]
    c=i-cb
    z=zip(s[i],s[j],s[j+(k-i)%c])
    print(*map(lambda x:((k-i)//c+1)*(x[0]-x[1])+x[2],z))


R=lambda: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]
    z=zip(s[i],s[j],s[j+(k-i)%(i-j)])
    print(*map(lambda x:((k-i)//(i-j)+1)*(x[0]-x[1])+x[2],z))


R=lambda: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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


R=lambda: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]
    c,d=divmod(k-i,i-j)
    z=zip(s[i],s[j],s[j+c])
    print(*map(lambda x:(d+1)*(x[0]-x[1])+x[2],z))


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()])
#ali = [[2, 2, 1], [3, 3, 1], [3, 1, 3]]
#bob = [[1, 1, 1], [2, 1, 1], [1, 2, 3]]
#k, a, b = 8, 1, 1
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)


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()])
#ali = [[2, 2, 1], [3, 3, 1], [3, 1, 3]]
#bob = [[1, 1, 1], [2, 1, 1], [1, 2, 3]]
#k, a, b = 8, 1, 1
res = [(a, b)]
sc = []
if a > b:
    sc.append((1, 0))
elif b > a:
    sc.append((0, 1))
else:
    sc.append((0, 0))
ci = -1
while True:
    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 > 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:]
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)


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
    #scoreB[(a, b)] = rb
elif (b - a) % 3 == 2:
    ra = 1
    #scoreA[(a, b)] = ra

i = 1
while i < k:
    na = A[a-1][b-1]
    nb = B[a-1][b-1]
    #print('game:', na, nb)

    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
        #print('Skip:', cycleLen, cycleScoreA, cycleScoreB, skipCycles)
        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
            #print(i, '+b', ra, rb)
        elif (nb - na) % 3 == 2:
            ra += 1
            #print(i, '+a', ra, rb)

        i += 1
        a, b = na, nb

#print('Answer')
print(ra, rb)
#print('ScoreA')
#print(scoreA)
#print('ScoreB')
#print(scoreB)
#print('pos')
#print(pos)


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)


R=lambda: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;
    if a-1==b%3:s[i][0]+=1
    elif b-1==a%3:s[i][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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


R=lambda: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
    if a-1==b%3:s[i][a-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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


R=lambda: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;
    print(a,b)
    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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


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 = 278
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)


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 = 137*18
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=map(int,input().split())
A=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
B=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
d=[(a,b)]
k-=1
l=[]
r=[]
if a>b:l.append(1);r.append(0)
elif a<b:r.append(1);l.append(0)
else:l.append(0);r.append(0)
while k:
    t=(A[a][b],B[a][b])
    a,b=A[a][b],B[a][b]
    if t not in d:
        d.append(t)
        if t[0]>t[1]:
            l.append(l[-1]+1);r.append(r[-1])
        elif t[0]<t[1]:l.append(l[-1]);r.append(r[-1]+1)
        else:l.append(l[-1]);r.append(r[-1])
    else:
        dd=d.index(t)
        L=len(d)-dd
        s=k//L
        k%=L
        l.append(l[-1]+(l[-1]-l[dd-1])*s)
        r.append(r[-1]+(r[-1] - r[dd - 1]) * s)
        l.append(l[-1]+l[dd+1]-l[dd-1])
        r.append(r[-1] + r[dd + 1] - r[dd - 1])
        break
    k-=1
print(l[-1],r[-1])


R=lambda: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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


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
#2520 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)


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 = 1251
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)


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 = 9
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=map(int,input().split())
A=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
B=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
d=[(a,b)]
k-=1
l=[]
r=[]
if a>b:l.append(1);r.append(0)
elif a<b:r.append(1);l.append(0)
else:l.append(0);r.append(0)
while k:
    t=(A[a][b],B[a][b])
    a,b=A[a][b],B[a][b]
    if t not in d:
        d.append(t)
        if t[0]>t[1]:
            l.append(l[-1]+1);r.append(r[-1])
        elif t[0]<t[1]:l.append(l[-1]);r.append(r[-1]+1)
        else:l.append(l[-1]);r.append(r[-1])
    else:
        dd=d.index(t)
        L=len(d)-dd
        s=k//L
        k%=L
        l.append(l[-1]+(l[-1]-l[dd-1])*s)
        r.append(r[-1]+(r[-1] - r[dd - 1]) * s)
        d.clear()
        k+=1
    k-=1
print(l[-1],r[-1])


R=lambda: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
    if a-1==b%3:s[i][1]+=1
    elif b-1==a%3:s[i][0]+=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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


R=lambda: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
    if a-1==b%3:s[i][(a-b+2)%3]+=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(*map(lambda x:(m+1)*(x[0]-x[1])+x[2],zip(s[i],s[j],s[j+n])))


k,a,b=map(int,input().split())
A=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
B=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
d=[(a,b)]
k-=1
l=[]
r=[]
h=lambda x,y:x-1==y%3
if h(a,b):l.append(1);r.append(0)
elif h(b,a):r.append(1);l.append(0)
else:l.append(0);r.append(0)
while k:
    t=(A[a][b],B[a][b])
    a,b=A[a][b],B[a][b]
    if t not in d:
        d.append(t)
        if h(t[0],t[1]):
            l.append(l[-1]+1);r.append(r[-1])
        elif h(t[1],t[0]):l.append(l[-1]);r.append(r[-1]+1)
        else:l.append(l[-1]);r.append(r[-1])
    else:
        dd=d.index(t)
        L=len(d)-dd
        s=k//L
        k%=L
        l.append(l[-1]+(l[-1]-l[dd-1])*s)
        r.append(r[-1]+(r[-1] - r[dd - 1]) * s)
        d.clear()
        k+=1
    k-=1
print(l[-1],r[-1])


k,a,b=map(int,input().split())
A=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
B=[0]+[[0]+[int(j)for j in input().split()]for i in range(1,4)]
d=[(a,b)]
k-=1
l=[]
r=[]
h=lambda x,y:x-1==y%3
if h(a,b):l.append(1);r.append(0)
elif a==b:r.append(0);l.append(0)
else:l.append(0);r.append(1)
while k:
    t=(A[a][b],B[a][b])
    a,b=A[a][b],B[a][b]
    if t not in d:
        d.append(t)
        if h(t[0],t[1]):
            l.append(l[-1]+1);r.append(r[-1])
        elif t[0]==t[1]:l.append(l[-1]);r.append(r[-1])
        else:l.append(l[-1]);r.append(r[-1]+1)
    else:
        dd=d.index(t)
        L=len(d)-dd
        s=k//L
        k%=L
        l.append(l[-1]+(l[-1]-l[dd-1])*s)
        r.append(r[-1]+(r[-1] - r[dd - 1]) * s)
        d.clear()
        k+=1
    k-=1
print(l[-1],r[-1])


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 = divmod(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=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)


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))
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 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))))


import sys

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

print(int(a_point), int(b_point))


import sys

def log(s):
#    print(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))


import sys

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

print(round(a_point), round(b_point))


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]))
##  print(i)
##  print((k-i) % (len(hm)-i))
##  print(' '.join((str(aa),str(bb))))
  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])
##  print(' '.join((str(aa),str(bb))))
  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(map(str,hm)))
##print(' '.join(map(str,hmhm)))
print(' '.join((str(aa),str(bb))))