# 奇数码问题

5 2 8
1 3
4 6 7

5 2 8
1 3
4 6 7

5 2
1 3 8
4 6 7

5 2 8
1 3 7
4 6

3
1 2 3
0 4 6
7 5 8
1 2 3
4 5 6
7 8 0
1
0
0

TAK
TAK

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

#define lowbit(x) x & (-x)
#define lson left,mid,root << 1
#define rson mid+1,right,root << 1 | 1

using namespace std;

typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const double eps=1e-8;
const int maxn=1e5+100;

int a[250005];
int b[250005];
int temp[250005];

ll mergesort(int a[], int l, int r)
{
if(l >= r)
return 0;

ll cnt = 0;

int mid = l + r >> 1;
cnt += mergesort(a, l, mid);
cnt += mergesort(a, mid + 1, r);

int k = 0;
int i = l;
int j = mid + 1;
while(i <= mid && j <= r)
{
if(a[i] <= a[j])
temp[k ++] = a[i ++];
else
{
cnt += mid - i + 1;
temp[k ++] = a[j ++];
}
}
while(i <= mid)
temp[k ++] = a[i ++];
while(j <= r)
temp[k ++] = a[j ++];
for(int i = l, j = 0; i <= r; i ++,j ++)
a[i] = temp[j];

return cnt;
}

int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
int x;
int num = 0;
for(int i = 1; i <= n * n; i ++)
{
scanf("%d",&x);
if(x)
a[++ num] = x;
}

num = 0;
for(int i = 1; i <= n * n; i ++)
{
scanf("%d",&x);
if(x)
b[++ num] = x;
}

ll cnta = mergesort(a, 1, n * n - 1);
ll cntb = mergesort(b, 1, n * n - 1);

if((cnta & 1) == (cntb & 1))
puts("TAK");
else
puts("NIE");
}
return 0;
}