#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int d[101010];
int ans[16];
int dp[1 << 16];
int dat[1 << 16];
int main()
{
	int num;
	scanf("%d", &num);
	for (int i = 0; i < num - 1; i++)
	{
		int za, zb, zc;
		scanf("%d%d%d", &za, &zb, &zc);
		d[za] ^= zc, d[zb] ^= zc;
	}
	for (int i = 0; i < num; i++)ans[d[i]]++;
	int r = 0, cnt = 0;
	int x = 0;
	for (int i = 1; i < 16; i++)r += ans[i] / 2, ans[i] %= 2, cnt += ans[i], x += ans[i] << i;
	ans[0] = 0;
	for (int i = 0; i < (1 << 16); i++)
	{
		for (int j = 0; j < 16; j++)if (i&(1 << j))dat[i] ^= j;
	}
	fill(dp, dp + (1 << 16), -1000000000);
	dp[0] = 0;
	for (int p = 0; p < (1 << 16); p++)
	{
		if ((x&p) != p)continue;
		int mask = (1 << 16) - 1 - p;
		int now = mask;
		for (;;)
		{
			if (now == 0)break;
			if (dat[now] == 0)dp[p + now] = max(dp[p + now], dp[p] + 1);
			now--;
			now &= mask;
		}
	}
	printf("%d\n", r + cnt - dp[x]);
}