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,unroll-loops")
using namespace std;
long long n, m, deg[200005], dsu[200005], size[200005], adj[800][800];
set<pair<long long, pair<long long, long long>>> e;
vector<pair<long long, pair<long long, long long>>> e2;
map<pair<long long, long long>, long long> mp;
time_t T = clock();
void init() {
  for (long long i = 1; i <= n; i++) dsu[i] = i, size[i] = 1;
}
long long find(long long k) { return k == dsu[k] ? k : k = find(dsu[k]); }
void Union(long long x, long long y) {
  if (x == y) return;
  if (size[x] < size[y])
    dsu[x] = y, size[y] += size[x];
  else
    dsu[y] = x, size[x] += size[y];
}
long long MST() {
  long long ans = 0, cnt = 0;
  for (auto i : e) {
    long long rx = find(i.second.first), ry = find(i.second.second);
    if (rx != ry) {
      Union(rx, ry);
      ans += i.first, cnt++;
    }
    if (cnt == n) break;
  }
  return ans;
}
signed main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  init();
  for (long long i = 1; i <= m; i++) {
    long long x, y, w;
    cin >> x >> y >> w;
    deg[x]++, deg[y]++;
    e.insert(make_pair(w, make_pair(min(x, y), max(x, y))));
  }
  if (n * (n - 1) / 2 - m >= n) {
    long long pre = 0;
    long long ans = 0, cnt = 1;
    for (long long i = 1; i <= n; i++)
      if (deg[i] < n - 1) {
        if (pre != 0) Union(find(i), find(pre)), cnt++;
        pre = i;
      }
    for (auto i : e) {
      long long rx = find(i.second.first), ry = find(i.second.second);
      if (rx != ry) {
        Union(rx, ry);
        ans += i.first;
      }
      if (cnt == n) break;
    }
    cout << ans;
  } else {
    long long XOR = 0, mx = 0;
    for (auto i : e) {
      adj[i.second.first][i.second.second] = 1;
      XOR ^= i.first;
      mx = max(mx, i.first);
    }
    long long res = 1000000000000000;
    vector<pair<long long, long long>> v;
    for (long long i = 1; i <= n; i++)
      for (long long j = i + 1; j <= n; j++) {
        if (!adj[i][j]) v.emplace_back(make_pair(i, j));
      }
    for (auto p : v) {
      if (clock() - T > 2.8 * CLOCKS_PER_SEC) break;
      e.insert(make_pair(XOR, p));
      init();
      for (auto q : v)
        if (p != q) Union(find(q.first), find(q.second));
      res = min(res, MST());
      e.insert(make_pair(0, p));
    }
    cout << res;
  }
}
