#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 3e6 + 7;
const ll mod = 1000000007;
#define mst(x, a) memset( x,a,sizeof(x) )
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
inline ll read() {
ll x = 0;
bool f = 0;
char ch = getchar();
while (ch < '0' || '9' < ch) f |= ch == '-', ch = getchar();
while ('0' <= ch && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
return f ? -x : x;
}
void out(ll x) {
int stackk[20];
if (x < 0) {
putchar('-');
x = -x;
}
if (!x) {
putchar('0');
return;
}
int top = 0;
while (x) stackk[++top] = x % 10, x /= 10;
while (top) putchar(stackk[top--] + '0');
}
int n, l[maxn], r[maxn], t[maxn][2], tot, head[maxn], cnt;
ll a[maxn];
struct node {
ll u, v, w, next;
} e[maxn];
void add(int u, int v, int w) {
e[cnt].v = v;
e[cnt].u = u;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
}
void insert(ll x, ll id) {
int p = 0;
for (int i = 32; i >= 0; i--) {
int op = (x >> i) & 1;
if (!t[p][op]) t[p][op] = ++tot;
p = t[p][op];
if (!l[p]) l[p] = id;
r[p] = id;
}
}
ll q(ll pp, ll x, ll pos) {
ll res = 0, p = pp;
for (int i = pos; i >= 0; i--) {
int op = (x >> i) & 1;
if (t[p][op]) p = t[p][op];
else {
res += (1 << i);
p = t[p][op ^ 1];
}
}
return res;
}
ll dfs(ll pp, ll pos) {
ll p = pp;
if (t[p][0] && t[p][1]) {
ll x = t[p][0];
ll y = t[p][1], minn = inf;
for (int i = l[x]; i <= r[x]; i++)
minn = min(minn, q(y, a[i], pos - 1) + (1 << pos));
return minn + dfs(t[p][1], pos - 1) + dfs(t[p][0], pos - 1);
} else if (t[p][1]) return dfs(t[p][1], pos - 1);
else if (t[p][0]) return dfs(t[p][0], pos - 1);
return 0;
}
void bfs(int u,int p,ll w) {
a[u]=w;
for(int i=head[u]; ~i; i=e[i].next) {
int v=e[i].v;
if(v==p) continue;
bfs(v,u,e[i].w^w);
}
}
int main() {
n = read();
mst(head, -1);
rep(i, 1, n - 1) {
int u = read();
int v = read();
ll w = read();
u++,v++;
add(u, v, w);
add(v, u, w);
}
bfs(1,1,0);
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) insert(a[i], i);
out(dfs(0, 32));
return 0;