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")
using namespace std;
template <typename T>
void read(T& x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
struct p {
  int u, v, w;
  bool operator<(const p& b) const { return w < b.w; }
};
p a[200010];
unordered_set<int, custom_hash> e[200010];
int colcnt;
bool vis[200010];
int fa[200010];
int col[200010], col_cnt[200010];
int same_col_e_cnt[200010];
int chs[200010];
bool gd[200010];
queue<int> ps;
void bfs(int);
int find(int);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n, m;
  int x, y, z;
  int xs = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    fa[i] = i;
    ps.push(i);
  }
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y >> z;
    e[x].insert(y);
    e[y].insert(x);
    a[i] = {x, y, z};
    xs ^= z;
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      vis[i] = true;
      col[i] = ++colcnt;
      col_cnt[colcnt] = 1;
      bfs(i);
    }
  }
  for (int i = 1; i <= m; ++i) {
    auto [x, y, z] = a[i];
    if (col[x] == col[y]) ++same_col_e_cnt[col[x]];
  }
  sort(a + 1, a + 1 + m);
  for (int i = 1; i <= n; ++i) fa[i] = col[i];
  long long sum = xs;
  bool del = false;
  for (int i = 1; i <= colcnt; ++i) {
    long long tmp = 1LL * col_cnt[i] * (col_cnt[i] - 1) / 2 - same_col_e_cnt[i];
    if (n == 633 && m == 199800)
      cout << i << ' ' << col_cnt[i] << ' '
           << 1LL * col_cnt[i] * (col_cnt[i] - 1) / 2 << ' '
           << same_col_e_cnt[i] << ' ' << sum << endl;
    assert(tmp >= col_cnt[i] - 1);
    if (!del && tmp != col_cnt[i] - 1) {
      del = true;
      sum = 0;
    }
  }
  for (int i = 1; i <= m; ++i) {
    auto [x, y, z] = a[i];
    int u = find(x);
    int v = find(y);
    int p = col[x];
    int q = col[y];
    if (u == v) {
      if (!del && xs > z) {
        if (p == q || gd[p] || gd[q]) {
          del = true;
          sum += z - xs;
        }
      }
      continue;
    }
    if (chs[p] && x != chs[p]) gd[p] = true;
    if (chs[q] && y != chs[q]) gd[q] = true;
    chs[p] = x;
    chs[q] = y;
    fa[u] = v;
    sum += z;
  }
  cout << sum;
  return 0;
}
void bfs(int st) {
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    auto x = q.front();
    int n = ps.size();
    q.pop();
    for (int i = 0; i < n; ++i) {
      int now = ps.front();
      ps.pop();
      if (vis[now]) continue;
      if (e[x].find(now) != e[x].end()) {
        ps.push(now);
      } else {
        col[now] = colcnt;
        ++col_cnt[colcnt];
        q.push(now);
        vis[now] = true;
      }
    }
  }
}
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
