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;
using i128 = __int128_t;
using u128 = __uint128_t;
const long long MOD = 998244353;
const long double PI = 3.141592653589793238;
const long long pi = 31415926;
const long long inf = 1000000000000000000;
const long long small_inf = INT_MAX;
long long int modpow(long long int x, long long int n,
                     long long int mod = MOD) {
  long long int res = 1;
  while (n > 0) {
    if (n & 1) res = res * x % mod;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}
long long int power(long long int x, long long int n) {
  long long int res = 1;
  while (n > 0) {
    if (n & 1) res = res * x;
    x = x * x;
    n >>= 1;
  }
  return res;
}
void init() {}
long long int ans = 0, c = 0;
;
int n, m;
void dfs2(vector<pair<int, long long int>> *v,
          vector<pair<int, long long int>> *v2, int x, std::vector<int> &vis,
          set<int> &rem) {
  auto aa = rem.begin();
  vis[x] = 1;
  std::vector<int> todo1;
  vis[x] = 1;
  for (auto p : v[x]) {
    while (aa != rem.end() && *aa < p.first) {
      todo1.push_back((*aa));
      aa++;
    }
    if (aa != rem.end() && *aa == p.first) {
      aa++;
    }
  }
  while (aa != rem.end()) {
    todo1.push_back((*aa));
    aa++;
  }
  for (auto aa : todo1) {
    rem.erase(aa);
  }
  for (auto y : todo1) {
    v2[x].push_back(make_pair(y, 0));
    v2[y].push_back(make_pair(x, 0));
  }
  for (auto y : todo1) {
    dfs2(v, v2, y, vis, rem);
  }
}
void merge(int x, int y, std::vector<int> &par, std::vector<int> &rank) {
  if (rank[x] < rank[y]) {
    par[x] = y;
  } else if (rank[x] == rank[y]) {
    par[y] = x;
    rank[x]++;
  } else {
    par[y] = x;
  }
  return;
}
int parent(int x, std::vector<int> &par, std::vector<int> &rank) {
  if (par[x] == x) return x;
  return par[x] = parent(par[x], par, rank);
}
void dfs(int x, vector<pair<int, long long int>> *v, std::vector<int> &par,
         std::vector<long long int> &par1, std::vector<int> &vis,
         std::vector<int> &lev) {
  vis[x] = 1;
  for (auto p : v[x]) {
    if (vis[p.first]) continue;
    int y = p.first;
    par[y] = x;
    par1[y] = p.second;
    lev[y] = lev[x] + 1;
    dfs(y, v, par, par1, vis, lev);
  }
}
void find(vector<pair<int, long long int>> *v, int n, long long int weight,
          int f) {
  vector<pair<long long int, std::pair<int, int>>> edge;
  for (int i = 1; i <= n; i++) {
    for (auto p : v[i]) {
      if (p.first > i)
        edge.push_back(make_pair(p.second, make_pair(i, p.first)));
    }
  }
  vector<pair<int, long long int>> v1[n + 1];
  std::vector<int> par(n + 1, 0), rank(n + 1, 1);
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
  long long int cnt = 0;
  long long int nn = n;
  long long int c1 = nn * (nn - 1ll) / 2ll;
  sort(edge.begin(), edge.end());
  std::vector<int> vis1((long long int)edge.size() + 1, 0);
  for (int i = 0; i < (long long int)edge.size(); i++) {
    auto p = edge[i];
    int x = p.second.first, y = p.second.second;
    if (p.first == 0) cnt++;
    int x1 = parent(x, par, rank), y1 = parent(y, par, rank);
    if (x1 != y1) {
      v1[x].push_back(make_pair(y, p.first));
      vis1[i] = 1;
      merge(x1, y1, par, rank);
      ans += p.first;
    }
  }
  if (cnt + c < c1) {
    cout << ans << endl;
    return;
  }
  vector<vector<std::pair<int, int>>> sparse(
      n + 1, std::vector<std::pair<int, int>>(21, make_pair(0, 0)));
  par = std::vector<int>(n + 1, 0);
  std::vector<int> vis(n + 1, 0), lev(n + 1, 1);
  std::vector<long long int> par1(n + 1, 0);
  dfs(1, v1, par, par1, vis, lev);
  for (int i = 1; i <= n; i++) {
    sparse[i][0] = make_pair(par[i], 0);
    if (par1[i] == 0) sparse[i][0].second = 1;
  }
  for (int i = 1; i <= 19; i++) {
    for (int j = 1; j <= n; j++) {
      if (sparse[j][i - 1].first == 0 ||
          sparse[sparse[j][i - 1].first][i - 1].first == 0)
        continue;
      sparse[j][i].first = sparse[sparse[j][i - 1].first][i - 1].first;
      sparse[j][i].second = max(sparse[j][i - 1].second,
                                sparse[sparse[j][i - 1].first][i - 1].second);
    }
  }
  if (m == 199395) {
    cout << edge.size() << endl;
  }
  for (int i = 0; i < (long long int)edge.size(); i++) {
    auto p = edge[i];
    if (vis1[i]) continue;
    if (p.first == 1323726413 - 1322034588 && m == 199395) {
      cout << i << " " << p.first << endl;
    }
  }
  for (int i = 0; i < (long long int)edge.size(); i++) {
    auto p = edge[i];
    if (vis1[i]) continue;
    int x = p.second.first, y = p.second.second;
    int f = 0;
    if (lev[x] < lev[y]) swap(x, y);
    for (int i = 19; i >= 0; i--) {
      if (lev[x] - power(2, i) >= lev[y]) {
        f = max(f, sparse[x][i].second);
        x = sparse[x][i].first;
      }
    }
    if (x == y) {
      if (f) {
        weight = min(weight, p.first);
        if (m == 199395) {
          cout << cnt << " " << c1 - c << endl;
          cout << ans << " " << weight << endl;
          cout << p.first << " " << p.second.first << " " << p.second.second
               << endl;
        }
        cout << ans + weight << endl;
        return;
      }
      continue;
    }
    for (int i = 19; i >= 0; i--) {
      if (sparse[x][i].first != sparse[y][i].first) {
        f = max(f, sparse[x][i].second);
        x = sparse[x][i].first;
        f = max(f, sparse[y][i].second);
        y = sparse[y][i].first;
      }
    }
    f = max(f, sparse[x][0].second);
    f = max(f, sparse[y][0].second);
    if (f == 1) {
      if (m == 199395) {
        cout << cnt << " " << c1 - c << endl;
        cout << ans << " " << weight << endl;
        cout << p.first << " " << p.second.first << " " << p.second.second
             << endl;
      }
      weight = min(weight, p.first);
      cout << ans + weight << endl;
      return;
    }
  }
  cout << ans + weight << endl;
}
void solve() {
  cin >> n >> m;
  long long int weight = 0;
  set<int> rem;
  for (int i = 1; i <= n; i++) {
    rem.insert(i);
  }
  vector<pair<int, long long int>> tmp[n + 1];
  vector<pair<int, long long int>> tmp2[n + 1];
  for (int i = 0; i < m; i++) {
    int x, y;
    long long int w;
    cin >> x >> y >> w;
    weight ^= w;
    tmp[x].push_back(make_pair(y, w));
    tmp[y].push_back(make_pair(x, w));
    tmp2[x].push_back(make_pair(y, w));
    tmp2[y].push_back(make_pair(x, w));
    c++;
  }
  for (int i = 1; i <= n; i++) {
    sort(tmp[i].begin(), tmp[i].end());
  }
  std::vector<int> vis(n + 1, 0);
  int n1 = 0;
  int f = 0;
  vis = std::vector<int>(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    rem.erase(i);
    dfs2(tmp, tmp2, i, vis, rem);
  }
  vector<pair<int, long long int>> *v = tmp2;
  find(v, n, weight, f);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t23 = 1, tt23 = 1;
  init();
  while (tt23 <= t23) {
    solve();
    tt23++;
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}
