#include<cstdio>
//#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
//#include<queue>
//#include<set>
#define INF 0x3f3f3f3f
#define N 250005
#define re register
#define Ii inline int
#define Il inline long long
#define Iv inline void
#define Ib inline bool
#define Id inline double
#define ll long long
#define Fill(a,b) memset(a,b,sizeof(a))
#define R(a,b,c) for(register int a=b;a<=c;++a)
#define nR(a,b,c) for(register int a=b;a>=c;--a)
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define Cmin(a,b) ((a)=(a)<(b)?(a):(b))
#define Cmax(a,b) ((a)=(a)>(b)?(a):(b))
#define D_e(x) printf("\n&__ %d __&\n",x)
#define D_e_Line printf("-----------------\n")
#define D_e_Matrix for(re int i=1;i<=n;++i){for(re int j=1;j<=m;++j)printf("%d ",g[i][j]);putchar('\n');}
using namespace std;
// The Code Below Is Bingoyes's Function Forest.
Ii read(){
int s=,f=;char c;
for(c=getchar();c>''||c<'';c=getchar())if(c=='-')f=-;
while(c>=''&&c<='')s=s*+(c^''),c=getchar();
return s*f;
}
Iv print(ll x){
if(x<)putchar('-'),x=-x;
if(x>)print(x/);
putchar(x%^'');
}
/*
Iv Floyd(){
R(k,1,n)
R(i,1,n)
if(i!=k&&dis[i][k]!=INF)
R(j,1,n)
if(j!=k&&j!=i&&dis[k][j]!=INF)
Cmin(dis[i][j],dis[i][k]+dis[k][j]);
}
Iv Dijkstra(int st){
priority_queue<int>q;
R(i,1,n)dis[i]=INF;
dis[st]=0,q.push((nod){st,0});
while(!q.empty()){
int u=q.top().x,w=q.top().w;q.pop();
if(w!=dis[u])continue;
for(re int i=head[u];i;i=e[i].nxt){
int v=e[i].pre;
if(dis[v]>dis[u]+e[i].w)
dis[v]=dis[u]+e[i].w,q.push((nod){v,dis[v]});
}
}
}
Iv Count_Sort(int arr[]){
int k=0;
R(i,1,n)
++tot[arr[i]],Cmax(mx,a[i]);
R(j,0,mx)
while(tot[j])
arr[++k]=j,--tot[j];
}
Iv Merge_Sort(int arr[],int left,int right,int &sum){
if(left>=right)return;
int mid=left+right>>1;
Merge_Sort(arr,left,mid,sum),Merge_Sort(arr,mid+1,right,sum);
int i=left,j=mid+1,k=left;
while(i<=mid&&j<=right)
arr[i]<=arr[j]?
tmp[k++]=arr[i++]:
tmp[k++]=arr[j++],sum+=mid-i+1;//Sum Is Used To Count The Reverse Alignment
while(i<=mid)tmp[k++]=arr[i++];
while(j<=right)tmp[k++]=arr[j++];
R(i,left,right)arr[i]=tmp[i];
}
Iv Bucket_Sort(int a[],int left,int right){
int mx=0;
R(i,left,right)
Cmax(mx,a[i]),++tot[a[i]];
++mx;
while(mx--)
while(tot[mx]--)
a[right--]=mx;
}
*/
int n,m,a[N],tmp[N];
Iv Merge_Sort(int arr[],int left,int right,int &sum){
if(left>=right)return;
int mid=left+right>>;
Merge_Sort(arr,left,mid,sum),Merge_Sort(arr,mid+,right,sum);
int i=left,j=mid+,k=left;
while(i<=mid&&j<=right)
(arr[i]<=arr[j])?
tmp[k++]=arr[i++]:
(tmp[k++]=arr[j++],sum+=mid-i+);//Sum Is Used To Count The Reverse Alignment
while(i<=mid)tmp[k++]=arr[i++];
while(j<=right)tmp[k++]=arr[j++];
R(i,left,right)arr[i]=tmp[i];
}
#define PutTAK printf("TAK\n")
#define PutNIE printf("NIE\n")
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int sum_start=,sum_end=;
n*=n;
if(!n)
PutNIE;
//Judge Case Of n=0 Specially
if(n==){
(read()==read())?
PutTAK:
PutNIE;
continue;
} //Judge Case Of n=1 Specially
int cnt_num=;
R(i,,n){
int num=read();
if(num)
a[++cnt_num]=num;
}
Merge_Sort(a,,cnt_num,sum_start);
cnt_num=;
R(i,,n){
int num=read();
if(num)
a[++cnt_num]=num;
}
Merge_Sort(a,,cnt_num,sum_end);
((sum_start&)==(sum_end&))?
PutTAK:
PutNIE;
}
return ;
}
/*
Note:
Error:
*/