#include<bits/stdc++.h>
using namespace std;
int n,s[1005],d[1005],c[1005],fang[1005];
int main(){
freopen("rotation.in","r",stdin);
freopen("rotation.out","w",stdout);
memset(fang,0x3f3f3f3f,sizeof(fang));
scanf("%d",&n);
for (int i=2;i<=n;i++){
scanf("%d%d%d",&s[i],&d[i],&c[i]);
}
fang[1]=0;
for (int i=2;i<=n;i++){
for (int j=2;j<=n;j++){
if (fang[s[j]]==1||fang[s[j]]==0){
if (c[j]==0) fang[d[j]]=fang[s[j]];
else{
if (fang[s[j]]==1) fang[d[j]]=0;
else fang[d[j]]=1;
}
}
}
}
printf("%d",fang[n]);
return 0;
}