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;
template <typename T>
inline void qr(T &x) {
  x = 0;
  int f = 0;
  char s = getchar();
  while (!isdigit(s)) f |= s == '-', s = getchar();
  while (isdigit(s)) x = x * 10 + s - 48, s = getchar();
  x = f ? -x : x;
}
int cc = 0, buf[31];
template <typename T>
inline void qw(T x) {
  if (x < 0) putchar('-'), x = -x;
  do {
    buf[++cc] = int(x % 10);
    x /= 10;
  } while (x);
  while (cc) putchar(buf[cc--] + '0');
  putchar(' ');
}
const int N = 2e5 + 10;
int n, m, fa[N], siz[N], sum[N];
vector<int> e[N];
int tp, sta[N], dd, del[N];
int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
set<int> s;
set<int>::iterator it;
struct edge {
  int x, y, z;
} a[N];
bool cmp(edge p1, edge p2) { return p1.z < p2.z; }
bool vis[N];
int main() {
  int cc = 0;
  qr(n), qr(m);
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    siz[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    qr(x), qr(y), qr(z);
    cc ^= z;
    a[i] = (edge){x, y, z};
    if (x > y) swap(x, y);
    e[y].push_back(x);
  }
  for (int i = 2; i <= n; i++)
    if (e[i].size() != i - 1) {
      for (int y : e[i]) vis[y] = 1;
      for (int j = 1; j <= i - 1; j++)
        if (!vis[j]) {
          e[i].push_back(j);
          a[++m] = (edge){j, i, cc};
        }
      break;
    }
  sort(a + 1, a + m + 1, cmp);
  s.insert(1);
  int done = 0;
  for (int i = 2; i <= n; i++) {
    for (int y : e[i]) {
      sum[findfa(y)]++;
      sta[++tp] = findfa(y);
    }
    for (it = s.begin(); it != s.end(); it++) {
      int now = (*it);
      if (sum[now] != siz[now]) {
        fa[now] = i;
        siz[i] += siz[now];
        done++;
        del[++dd] = now;
      }
    }
    while (dd) s.erase(del[dd--]);
    s.insert(i);
    while (tp) sum[sta[tp--]] = 0;
  }
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    int x = a[i].x, y = a[i].y;
    if (findfa(x) != findfa(y))
      fa[findfa(x)] = findfa(y), ans += (long long)a[i].z;
  }
  cout << ans << endl;
  return 0;
}
