#include<bits/stdc++.h>

using namespace std;

int n,x,y,z,now,ans,a[120000],cnt[16],f[1<<15];
vector<int> vec;

int main(){
	scanf("%d",&n);
	for (int i=1;i<n;++i){
		scanf("%d%d%d",&x,&y,&z);
		a[x]^=z; a[y]^=z;
	}
	for (int i=0;i<n;++i) ++cnt[a[i]];
	for (int i=1;i<=15;++i){
		ans+=cnt[i]/2;
		cnt[i]%=2;
		now|=cnt[i]<<(i-1);
	}
	memset(f,-1,sizeof f);
	f[now]=ans; vec.push_back(now);
	for (int i=0;i<(int)vec.size();++i){
		int S=vec[i];
		for (int j1=1;j1<=15;++j1)
			if (1<<(j1-1)&S){
				for (int j2=j1+1;j2<=15;++j2)
					if (1<<(j2-1)&S){
						for (int k=1;k<=15;++k){
							int t1=j1^k,t2=j2^k,T=S,tmp=1;
							T^=1<<(j1-1); T^=1<<(j2-1);
							if (1<<(t1-1)&T) ++tmp;
							if (1<<(t2-1)&T) ++tmp;
							if (t1) T^=1<<(t1-1); if (t2) T^=1<<(t2-1);
							if (f[T]==-1||f[S]+tmp<f[T]){
								f[T]=f[S]+tmp;
								vec.push_back(T);
							}
						}
					}
			}
	}
	printf("%d\n",f[0]);
	
	return 0;
}
			