#include<iostream> #include<cstdlib> #include<cstdio> usingnamespacestd; inlineintread() { int f = 1, x = 0; char ch;
do{ ch = getchar(); if (ch == '-') f = -1; }while(ch < '0' || ch > '9'); do{ x = x * 10 + ch - '0'; ch = getchar(); }while(ch >= '0' && ch <= '9'); return f * x; } constint N = 1e5;
int n; int val[N + 1], sum[16], state; int can[1 << 15], f[1 << 15]; int ans;
intmain() { n = read(); for (int i = 1; i < n; i++) { int u = read() + 1, v = read() + 1, w = read(); val[u] ^= w; val[v] ^= w; } for (int i = 1; i <= n; i++) sum[val[i]]++;
for (int i = 1; i < 16; i++) { ans += sum[i] >> 1; sum[i] &= 1; state |= sum[i] << (i - 1); } for (int S = 0; S < 1 << 15; S++) { int sum = 0, tot = 0; for (int i = 1; i <= 15; i++) { if (S & (1 << (i - 1))) { sum ^= i; tot++; } } if (!sum) can[S] = 1; f[S] = tot - 1; } f[0] = 0; for (int S = 0; S < 1 << 15; S++) { for (int T = (S - 1) & S; T; T = (T - 1) & S) if (can[T]) f[S] = min(f[S], f[T] + f[S ^ T]); }