#include<bits/stdc++.h>
using namespace std;
int const N=1e5+1,base=N+2,mod=1e9+7;
bool odw[N];
long long hsh1[N],hsh2[N],poz[N];
vector<int>g1[N],g2[N];
void dfs1(int v){
odw[v]=1;
for(int i:g1[v]){
if(odw[i]==0){
poz[i]=poz[v]+1;
dfs1(i);
hsh1[v]=(hsh1[v]+hsh1[i])%mod;
}
}
hsh1[v]=hsh1[v]*poz[v]*base%mod;
}
void dfs2(int v){
odw[v]=1;
for(int i:g2[v]){
if(odw[i]==0){
poz[i]=poz[v]+1;
dfs2(i);
hsh2[v]=(hsh2[v]+hsh2[i])%mod;
}
}
hsh2[v]=hsh2[v]*poz[v]*base%mod;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,a,b;
cin>>n;
for(int i=1;i<n;i++){
cin>>a>>b;
g1[a].push_back(b);
g1[b].push_back(a);
}
for(int i=1;i<n;i++){
cin>>a>>b;
g2[a].push_back(b);
g2[b].push_back(a);
}
poz[1]=1;
dfs1(1);
for(int i=0;i<=n;i++) odw[i]=0, poz[i]=0;
poz[1]=1;
dfs2(1);
if(hsh1[1]==hsh2[1]) cout<<"TAK";
else cout<<"NIE";
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGNvbnN0IE49MWU1KzEsYmFzZT1OKzIsbW9kPTFlOSs3Owpib29sIG9kd1tOXTsKbG9uZyBsb25nIGhzaDFbTl0saHNoMltOXSxwb3pbTl07CnZlY3RvcjxpbnQ+ZzFbTl0sZzJbTl07Cgp2b2lkIGRmczEoaW50IHYpewogICAgb2R3W3ZdPTE7CiAgICBmb3IoaW50IGk6ZzFbdl0pewogICAgICAgIGlmKG9kd1tpXT09MCl7CiAgICAgICAgCXBveltpXT1wb3pbdl0rMTsKICAgICAgICAgICAgZGZzMShpKTsKICAgICAgICAgICAgaHNoMVt2XT0oaHNoMVt2XStoc2gxW2ldKSVtb2Q7CiAgICAgICAgfQogICAgfQogICAgaHNoMVt2XT1oc2gxW3ZdKnBvelt2XSpiYXNlJW1vZDsKfQoKdm9pZCBkZnMyKGludCB2KXsKICAgIG9kd1t2XT0xOwogICAgZm9yKGludCBpOmcyW3ZdKXsKICAgICAgICBpZihvZHdbaV09PTApewogICAgICAgIAlwb3pbaV09cG96W3ZdKzE7CiAgICAgICAgICAgIGRmczIoaSk7CiAgICAgICAgICAgIGhzaDJbdl09KGhzaDJbdl0raHNoMltpXSklbW9kOwogICAgICAgIH0KICAgIH0KICAgIGhzaDJbdl09aHNoMlt2XSpwb3pbdl0qYmFzZSVtb2Q7Cn0KCmludCBtYWluKCl7Cglpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOwoJY2luLnRpZSgwKTsKICAgIGludCBuLGEsYjsKICAgIGNpbj4+bjsKICAgIGZvcihpbnQgaT0xO2k8bjtpKyspewogICAgICAgIGNpbj4+YT4+YjsKICAgICAgICBnMVthXS5wdXNoX2JhY2soYik7CiAgICAgICAgZzFbYl0ucHVzaF9iYWNrKGEpOwogICAgfQogICAgZm9yKGludCBpPTE7aTxuO2krKyl7CiAgICAgICAgY2luPj5hPj5iOwogICAgICAgIGcyW2FdLnB1c2hfYmFjayhiKTsKICAgICAgICBnMltiXS5wdXNoX2JhY2soYSk7CiAgICB9CiAgICBwb3pbMV09MTsKICAgIGRmczEoMSk7CiAgICBmb3IoaW50IGk9MDtpPD1uO2krKykgb2R3W2ldPTAsIHBveltpXT0wOwogICAgcG96WzFdPTE7CiAgICBkZnMyKDEpOwogICAgaWYoaHNoMVsxXT09aHNoMlsxXSkgY291dDw8IlRBSyI7CiAgICBlbHNlIGNvdXQ8PCJOSUUiOwp9Cg==