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>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
int n, k, m, du[2000200];
long long nd;
vector<pair<int, int> > e[2000200];
vector<pair<int, int> > p;
int fa[2000200];
int mp[1010][1010];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
vector<pair<int, pair<int, int> > > E;
int WW;
inline long long work(vector<pair<int, pair<int, int> > > e) {
  for (auto i = (1); i <= (n); ++i) fa[i] = i;
  sort(e.begin(), e.end());
  long long res = 0;
  for (auto [w, v] : e) {
    int p = find(v.first), q = find(v.second);
    if (p != q) {
      res += w;
      WW = w;
      fa[p] = q;
    }
  }
  return res;
}
inline void solve() {
  scanf("%d%d", &n, &m);
  long long nd = (1ll * n * (n - 1) / 2) - m - 1;
  int P = 0;
  for (auto i = (1); i <= (m); ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    P ^= w;
    e[u].push_back({v, w});
    e[v].push_back({u, w});
    ++du[u], ++du[v];
    E.push_back({w, {u, v}});
  }
  for (auto i = (1); i <= (n); ++i)
    if (du[i] != n - 1) p.push_back({du[i], i});
  sort(p.begin(), p.end());
  for (auto i = (1); i <= (n); ++i) fa[i] = i;
  long long ans = 1e18;
  if (nd < p.size() - 1) {
    for (auto i = (1); i <= (n); ++i)
      for (auto v : e[i]) {
        mp[i][v.first] = mp[v.first][i] = 1;
      }
    vector<pair<int, int> > o;
    o.clear();
    int ct = 0;
    for (auto i = (1); i <= (n); ++i)
      for (auto j = (i + 1); j <= (n); ++j) {
        if (i == j) continue;
        if (!mp[i][j]) {
          o.push_back({i, j});
          mp[j][i] = 1;
          ++ct;
        }
      }
    vector<pair<int, pair<int, int> > > E1 = E;
    for (auto j = (1); j <= (ct - 1); ++j) {
      E1.push_back({0, {o[j - 1]}});
    }
    ans = work(E1);
    if (WW > P)
      ans -= WW, ans += P;
    else
      ans += P;
    printf("%lld\n", ans);
  } else {
    ans = 0;
    if (p.size())
      for (auto v : p) fa[v.second] = p[0].second;
    sort(E.begin(), E.end());
    for (auto [w, v] : E) {
      int p = find(v.first), q = find(v.second);
      if (p != q) {
        ans += w;
        fa[p] = q;
      }
    }
    printf("%lld\n", ans);
  }
}
int main() {
  int T = 1;
  while (T--) solve();
}
