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;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
const long long N = 2e5 + 100, inf = (long long long long)1e18 + 100;
vector<long long> g[N];
set<pair<long long, long long> > s;
vector<long long> q[N];
long long val[N];
bool mark[N];
long long long long mol[N];
map<pair<long long long long, long long long long>, long long> mp;
vector<pair<long long, pair<long long long long, long long long long> > > yal;
long long long long par[N];
long long long long getpar(long long long long v) {
  if (par[v] == v) return v;
  return par[v] = getpar(par[v]);
}
int32_t main() {
  ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);
  long long n, m;
  cin >> n >> m;
  long long long long xo = 0;
  for (long long i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
    mp[{u, v}] = w;
    mp[{v, u}] = w;
    xo ^= w;
  }
  for (long long i = 0; i < n; i++) {
    s.insert({val[i], i});
  }
  long long cnt = 0;
  long long k = 0;
  while (s.size()) {
    long long v = s.begin()->second;
    q[k].push_back(v);
    mark[v] = true;
    s.erase(s.begin());
    for (long long i = 0; i < q[k].size(); i++) {
      v = q[k][i];
      cnt++;
      for (long long j = 0; j < g[v].size(); j++) {
        long long u = g[v][j];
        if (!mark[u]) {
          s.erase({val[u], u});
          val[u] = cnt;
          s.insert({val[u], u});
        }
      }
      while (s.size() and s.begin()->first < cnt) {
        q[k].push_back(s.begin()->second);
        mark[s.begin()->second] = true;
        s.erase(s.begin());
      }
    }
    k++;
  }
  for (long long i = 0; i < k; i++) {
    for (long long j = 0; j < q[i].size(); j++) {
      mol[q[i][j]] = i;
    }
  }
  long long long long ans = 0;
  for (long long i = 0; i < k; i++) {
    for (long long j = i + 1; j < k; j++) {
      long long long long mn = (long long long long)1e15;
      for (auto u : q[i]) {
        for (auto v : q[j]) {
          mn = min(mn, mp[{u, v}]);
        }
      }
      yal.push_back({mn, {i, j}});
    }
  }
  sort(yal.begin(), yal.end());
  for (long long i = 0; i < k; i++) par[i] = i;
  for (auto U : yal) {
    long long long long u = U.second.first, v = U.second.second, w = U.first;
    u = getpar(u);
    v = getpar(v);
    if (u != v) {
      par[u] = v;
      ans += w;
    }
  }
  long long long long mn = (long long long long)1e15;
  long long long long z = n * (n - 1) / 2 - m;
  for (long long i = 0; i < k; i++) {
    for (auto v : q[i]) {
      for (auto u : g[v]) {
        mn = min(mn, mp[{u, v}]);
      }
    }
    z -= q[i].size() - 1;
  }
  if (z == 0) ans += min(mn, xo);
  return cout << ans << '\n', 0;
  ;
  return 0;
}
