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;
vector<pair<int, long long> > nxt[600001];
int n, m;
set<int> nt[600001];
set<int> S;
int h;
int col[600001];
void DFS(int u, int co) {
  col[u] = co;
  S.erase(S.find(u));
  auto p = S.begin();
  while (p != S.end()) {
    int tar = *p;
    if (nt[u].find(tar) == nt[u].end()) {
      DFS(tar, co);
    }
    p = S.upper_bound(tar);
  }
}
void flipG() {
  for (int i = 1; i <= n; i++) S.insert(i);
  for (int i = 1; i <= n; i++)
    if (S.find(i) != S.end()) {
      h++;
      DFS(i, h);
    }
}
vector<pair<int, long long> > nxt2[600001];
long long ret = 0;
long long xx = 0;
priority_queue<pair<int, int> > Q;
int vis[600001];
long long dist[600001];
void MST() {
  Q.push(make_pair(0, 1));
  while (Q.size()) {
    auto W = Q.top();
    Q.pop();
    int u = W.second;
    long long we = -W.first;
    if (!vis[u]) {
      ret += we;
      vis[u] = 1;
      for (auto x : nxt2[u]) {
        int tar = x.first;
        long long w = x.second;
        Q.push(make_pair(-w, tar));
      }
    }
  }
}
vector<pair<int, pair<int, int> > > E;
int head[600001], cnt[600001];
int has[4001][4001];
int f(int u) {
  if (head[u] == u) return u;
  return (head[u] = f(head[u]));
}
void ui(int u, int v) {
  if (cnt[u] > cnt[v]) {
    ui(v, u);
    return;
  }
  head[u] = v;
  cnt[v] += cnt[u];
}
int MST2() {
  for (int i = 1; i <= n; i++)
    for (auto x : nxt[i]) {
      E.push_back(make_pair(x.second, make_pair(i, x.first)));
      has[i][x.first] = has[x.first][i] = 1;
    }
  sort(E.begin(), E.end());
  for (int i = 1; i <= n; i++) head[i] = i, cnt[i] = 1;
  for (auto e : E) {
    int w = e.first;
    int u = e.second.first;
    int v = e.second.second;
    int fu = f(u);
    int fv = f(v);
    if (w < xx) {
      if (fu != fv) {
        ui(fu, fv);
      }
      for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
          if (col[i] == col[j] && f(i) == f(j)) return w;
    }
  }
  return xx;
}
void main2() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    long long w;
    scanf("%d%d%lld", &u, &v, &w);
    nxt[u].push_back(make_pair(v, w));
    nxt[v].push_back(make_pair(u, w));
    nt[u].insert(v);
    nt[v].insert(u);
    xx ^= w;
  }
  flipG();
  long long tot = (long long)n * (n - 1) / 2;
  long long fr = tot - m;
  long long req = n - h;
  int mm = 0;
  for (int i = 1; i <= n; i++) {
    for (auto x : nxt[i]) {
      int j = x.first;
      int w = x.second;
      if (col[i] != col[j]) {
        mm++;
        nxt2[col[i]].push_back(make_pair(col[j], w));
      }
    }
  }
  MST();
  if (fr > req) {
    printf("%lld\n", ret);
    return;
  } else {
    int tot = MST2();
    printf("%lld\n", ret + tot);
  }
}
int main() { main2(); }
