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;
const int N = 5e6 + 5;
struct Edge {
  int u, v, w;
  inline friend bool operator<(const Edge& a, const Edge& b) {
    return a.w < b.w;
  }
} e[N];
int n, m, fa[N], tot = n, num, nxt[N];
vector<int> g[N];
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
void merge(int u, int v) {
  u = find(u), v = find(v);
  if (u == v) return;
  fa[u] = v, --num;
}
int main() {
  scanf("%d%d", &n, &m), num = n;
  for (int i = 1; i <= n; i++) {
    fa[i] = i, nxt[i] = i;
  }
  int res = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
    g[e[i].u].push_back(e[i].v);
    g[e[i].v].push_back(e[i].u);
    res ^= e[i].w;
  }
  for (int i = 1; i <= n; i++) {
    sort(g[i].begin(), g[i].end());
    g[i].push_back(n + 1);
    int pre = 1;
    for (auto u : g[i]) {
      if (pre + 1 <= u - 1) {
        nxt[pre + 1] = max(nxt[pre + 1], u - 1);
        merge(i, pre + 1);
      }
      pre = u;
    }
  }
  int pre = n + 1;
  for (int i = n; i >= 1; i--) {
    if (nxt[i] == i) continue;
    for (int j = i + 1; j <= min(pre, nxt[i]); j++) {
      merge(i, j);
    }
    pre = i;
  }
  if (num == 1 && n * (n - 1) / 2 - m > n - 1) {
    puts("0");
    return 0;
  }
  sort(e + 1, e + m + 1);
  bool changed = false;
  long long ans = res;
  if (n == 632) {
    for (int i = 1; i <= n; i++) {
      if (fa[i] != i) {
        printf("fa[%d] = %d\n", i, fa[i]);
      }
    }
  }
  printf("res = %d\n", res);
  for (int i = 1; i <= m; i++) {
    e[i].u = find(e[i].u), e[i].v = find(e[i].v);
    if (e[i].u == e[i].v) {
      if (!changed && e[i].w < res) {
        changed = true;
        ans += e[i].w - res;
      }
      continue;
    }
    fa[e[i].u] = e[i].v, --num;
    ans += e[i].w;
  }
  printf("%lld\n", ans);
  return 0;
}
