2 0
 6 8
1 4 1
1 5 1
1 6 1
2 4 1
2 5 1
2 6 1
3 5 1
3 6 1
 6 12
1 3 8763000
1 4 8763000
1 5 8763000
1 6 8763000
2 3 8763000
2 4 1
2 5 8763000
2 6 1
3 5 8763000
3 6 8763000
4 5 8763000
4 6 2

0
 0
 8763002

#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long w = 0, f = 1;
  char c = ' ';
  while (c < '0' || c > '9') f = (c == '-' ? -f : f), c = getchar();
  while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
  return w * f;
}
struct node {
  long long u, v, w;
} t[200005 << 1];
long long n, m, w, f[200005], mp[650][650], deg[200005], ans = 1e18;
long long find(long long x) { return f[x] == x ? x : f[x] = find(f[x]); }
void init() {
  for (long long i = 1; i <= n; i++) f[i] = i;
}
void solve1() {
  init();
  memset(mp, -1, sizeof(mp));
  long long res = 0, cnt = 0;
  for (long long i = 1; i <= m; i++)
    mp[t[i].u][t[i].v] = mp[t[i].v][t[i].u] = t[i].w;
  for (long long i = 1; i <= n; i++)
    for (long long j = i + 1; j <= n; j++)
      if (mp[i][j] == -1) {
        long long fx = find(i), fy = find(j);
        if (fx != fy) f[fx] = fy, cnt++;
      }
  if (cnt == n * (n - 1) / 2 - m) res = w;
  for (long long i = 1; i <= m; i++) {
    long long fx = find(t[i].u), fy = find(t[i].v);
    if (fx != fy) f[fx] = fy, res += t[i].w;
  }
  ans = min(ans, res);
}
map<long long, bool> vis[200005];
void solve2() {
  init();
  for (long long i = 1; i <= m; i++)
    deg[t[i].u]++, deg[t[i].v]++, vis[t[i].u][t[i].v] = vis[t[i].v][t[i].u] = 1;
  long long minn = n + 1, minid = 0, res = 0, cnt = 0;
  for (long long i = 1; i <= n; i++)
    if (deg[i] < minn) minn = deg[i], minid = i;
  for (long long i = 1; i <= n; i++) {
    long long fx = find(minid), fy = find(i);
    if (fx != fy && !vis[minid][i] && cnt != n * (n - 1) / 2 - m - 1)
      f[fx] = fy, cnt++;
  }
  for (long long i = 1; i <= n; i++)
    if (find(i) != find(minid))
      for (long long j = 1; j <= n; j++) {
        long long fx = find(i), fy = find(j);
        if (fx != fy && !vis[i][j] && cnt != n * (n - 1) / 2 - m - 1)
          f[fx] = fy, cnt++;
      }
  for (long long i = 1; i <= m; i++) {
    long long fx = find(t[i].u), fy = find(t[i].v);
    if (fx != fy) f[fx] = fy, res += t[i].w;
  }
  ans = min(ans, res);
}
signed main() {
  n = read(), m = read();
  for (long long i = 1; i <= m; i++)
    t[i].u = read(), t[i].v = read(), t[i].w = read(), w ^= t[i].w;
  sort(t + 1, t + m + 1, [&](node x, node y) { return x.w < y.w; });
  if (n * (n - 1) / 2 < 200005 * 2) solve1();
  solve2();
  printf("%lld\n", ans);
  return 0;
}
