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 = 4e5 + 5;
int n, m, fa[N], sz[N];
inline int find(int x) {
  while (x != fa[x]) x = fa[x] = fa[fa[x]];
  return x;
}
struct EDGE {
  int u, v, w;
  inline bool operator<(const EDGE& rhs) const { return w < rhs.w; }
} E[N];
namespace sub1 {
int pre[N], nxt[N], deg[N], head[N], cnt;
unsigned long long ans;
struct edge {
  int to, nxt;
} e[N * 2];
void solve(int u) {
  static int g[N];
  vector<int> st, del;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = find(e[i].to);
    ++g[v];
    st.emplace_back(v);
  }
  for (int v = nxt[0]; v != n + 1; v = nxt[v]) {
    int x = find(u), y = find(v);
    if (x != y && sz[y] != g[y]) {
      fa[y] = x;
      sz[x] += sz[y];
      del.emplace_back(v);
    }
  }
  for (int x : del) {
    nxt[pre[x]] = nxt[x];
    pre[nxt[x]] = pre[x];
  }
  for (int x : st) --g[x];
}
void work() {
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    E[i].u = u, E[i].v = v, E[i].w = w;
    e[++cnt] = (edge){v, head[u]}, head[u] = cnt;
    e[++cnt] = (edge){u, head[v]}, head[v] = cnt;
    ++deg[u], ++deg[v];
  }
  int u = min_element(deg + 1, deg + n + 1) - deg;
  for (int i = 1; i <= n; ++i) pre[i] = i - 1, nxt[i] = i + 1;
  nxt[0] = 1, pre[n + 1] = n;
  solve(u);
  for (int i = 1; i <= n; ++i)
    if (i != u) solve(i);
  sort(E + 1, E + m + 1);
  for (int i = 1; i <= m; ++i) {
    int u = find(E[i].u), v = find(E[i].v);
    if (u != v) {
      fa[v] = u, ans += E[i].w;
    }
  }
  cout << ans << '\n';
}
}  // namespace sub1
namespace sub2 {
unsigned long long ans;
bool b[800][800];
int head[800], cnt;
struct edge {
  int to, nxt, w;
} e[2333];
bool check(int u, int t, int p, bool ok) {
  if (u == t) return ok;
  for (int i = head[u]; i; i = e[i].nxt)
    if (e[i].to != p) {
      if (check(e[i].to, t, u, ok || e[i].w)) return 1;
    }
  return 0;
}
void work() {
  for (int i = 1; i <= m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    b[u][v] = b[v][u] = 1;
    E[i].u = u, E[i].v = v, E[i].w = w;
    ans ^= w;
  }
  bool flag = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      if (!b[i][j]) {
        int u = find(i), v = find(j);
        if (u != v) {
          fa[v] = u;
          e[++cnt] = (edge){j, head[i], 0}, head[i] = cnt;
          e[++cnt] = (edge){i, head[j], 0}, head[j] = cnt;
        } else
          flag = 1;
      }
  sort(E + 1, E + m + 1);
  int k = ans;
  ans = 0;
  for (int i = 1; i <= m; ++i) {
    int u = find(E[i].u), v = find(E[i].v);
    if (u != v) {
      fa[v] = u, ans += E[i].w;
      e[++cnt] = (edge){E[i].v, head[E[i].u], E[i].w};
      head[E[i].u] = cnt;
      e[++cnt] = (edge){E[i].u, head[E[i].v], E[i].w};
      head[E[i].v] = cnt;
    } else if (E[i].w < k && !flag && check(u, v, 0, 0)) {
      flag = 1;
      ans += E[i].w;
    }
  }
  if (!flag) ans += k;
  cout << ans << '\n';
}
}  // namespace sub2
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;
  if (n * (n - 1LL) / 2 > m + n)
    sub1::work();
  else
    sub2::work();
  return 0;
}
