#define lowbit(x) (x & -x) #define int long long #define endl '\n'
usingnamespace std;
int n, a[250009], c[250009];
voidadd(int x){ for (int i = x; i <= n * n; i += lowbit(i)) { c[i] += 1; } }
intquery(int x){ int res = 0; for (int i = x; i; i -= lowbit(i)) { res += c[i]; } return res; }
intwork(int num){ memset(c, 0, sizeof(c)); num *= num; for (int x, cnt = 1, i = 1; i <= num; i++) { cin >> x; if (x) { a[cnt++] = x; } } int res = 0; for (int i = 1; i < num; i++) { res += i - 1 - query(a[i]); add(a[i]); } return res; }
signedmain(){ while (cin >> n) { int a = work(n), b = work(n); cout << ((a & 1) == (b & 1) ? "TAK\n" : "NIE\n"); } }