#include <bits/stdc++.h>

using namespace std;

#define int int64_t

const int maxn = 1e5 + 42;

vector<pair<int, int>> g[maxn];

void make_edge(int u, int v, int c) {
    g[u].push_back({v, c});
    g[v].push_back({u, c});
}

int xr[4 * maxn];

int get(int a, int b, int v = 1, int l = 0, int r = maxn) {
    if(a <= l && r <= b) {
        return xr[v];
    } else if(r <= a || b <= l) {
        return 0;
    } else {
        int m = (l + r) / 2;
        return get(a, b, 2 * v, l, m) ^ get(a, b, 2 * v + 1, m, r);
    }
}

void upd(int p, int c, int v = 1, int l = 0, int r = maxn) {
    xr[v] ^= c;
    if(r - l == 1) {
        return;
    }
    int m = (l + r) / 2;
    if(p < m) {
        upd(p, c, 2 * v, l, m);
    } else {
        upd(p, c, 2 * v + 1, m, r);
    }
}

int z[maxn];
int in[maxn], out[maxn], t;
void dfs(int v, int p) {
    in[v] = t++;
    for(auto it: g[v]) {
        int u = it.first;
        int x = it.second;
        if(u != p) {
            dfs(u, v);
            z[u] = x;
        }
    }
    out[v] = t;
}

int deg[maxn];

map<vector<int>, int> ans;
vector<int> zmasks;
int solve(vector<int> &need) {
    if(!ans.count(need)) {
        int res = accumulate(begin(need), end(need), 0);
        for(auto it: zmasks) {
            int t = 1e6;
            for(int i = 0; i < 16; i++) {
                if((it >> i) & 1) {
                    t = min(t, need[i]);
                }
            }
            for(int i = 0; i < 16; i++) {
                if((it >> i) & 1) {
                    need[i] -= t;
                }
            }
            if(t) {
                res = min(res, solve(need) + t * (__builtin_popcount(it) - 1));
            }
            for(int i = 0; i < 16; i++) {
                if((it >> i) & 1) {
                    need[i] += t;
                }
            }
        }
        ans[need] = res;
    }
    return ans[need];
}

signed main() {
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    int x[n - 1], y[n - 1], a[n - 1];
    for(int i = 0; i < n - 1; i++) {
        cin >> x[i] >> y[i] >> a[i];
        make_edge(x[i], y[i], a[i]);
    }
    dfs(0, 0);
    deque<int> que;
    for(int i = 1; i < n; i++) {
        deg[i] = g[i].size();
        if(deg[i] == 1) {
            que.push_back(i);
        }
    }
    vector<int> need(16);
    while(!que.empty()) {
        int v = que.front();
        que.pop_front();
        deg[v]--;
        for(auto it: g[v]) {
            int u = it.first;
            if(u > 0 && deg[u] > 0) {
                deg[u]--;
                if(deg[u] == 1) {
                    que.push_back(u);
                }
            }
        }
        int t = get(in[v], out[v]);
        need[t ^ z[v]]++;
        upd(in[v], t ^ z[v]);
    }
    need[0] = 0;
    int mask_sz = 1 << 16;
    int zero[mask_sz];
    memset(zero, 0, sizeof(zero));
    for(int mask = 2; mask < mask_sz; mask += 2) {
        int cur = 0;
        for(int i = 0; i < 16; i++) {
            if((mask >> i) & 1) {
                cur ^= i;
            }
        }
        zero[mask] = cur == 0;
        for(int sub = mask & (mask - 1); sub; sub = (sub - 1) & mask) {
            if(zero[sub]) {
                zero[mask] = 0;
                break;
            }
        }
        if(zero[mask]) {
            zmasks.push_back(mask);
        }
    }
    int ans = 0;
    for(int i = 0; i < 16; i++) {
        ans += need[i] / 2;
        need[i] %= 2;
    }
    cout << ans + solve(need) << endl;
    return 0;
}


Measured memory_bytes: 22400