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("O3", "unroll-all-loops")
#pragma GCC target("sse4.2")
using namespace std;
ifstream in;
ofstream out;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
const long long inf = ml * ml * ml + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long n, m;
vector<vector<pair<int, int>>> e;
vector<vector<int>> E;
vector<set<long long>> es;
vector<long long> unused, d;
long long X, MN;
long long zero;
vector<pair<long long, pair<long long, long long>>> all;
bool nice = false;
bool viv = false;
set<pair<int, int>> freed;
void DFS(int v, int c = 0) {
  d[v] = c;
  vector<long long> still;
  vector<long long> fr;
  for (auto u : unused) {
    if (es[v].find(u) == es[v].end()) {
      if (u != v) {
        E[u].push_back(v);
        E[v].push_back(u);
        freed.insert({u, v});
        freed.insert({v, u});
      }
      fr.push_back(u);
    } else
      still.push_back(u);
  }
  unused = still;
  for (auto u : fr)
    if (u != v) DFS(u, c);
}
struct DSU {
  vector<int> p;
  int n;
  DSU(int _n) { build(_n); }
  void build(int _n) {
    n = _n;
    p.resize(n);
    for (int i = 0; i < n; i++) p[i] = i;
  }
  int pp(int v) {
    if (v == p[v]) return v;
    return p[v] = pp(p[v]);
  }
  bool uni(int u, int v) {
    u = pp(u);
    v = pp(v);
    if (u == v) return false;
    p[v] = u;
    return true;
  }
};
const int K = 20;
vector<int> p, cnt, dd;
vector<int> pp[K];
void DFS2(int v, int now) {
  cnt[v] = now;
  for (auto u : E[v])
    if (p[u] == -1) {
      int will = now;
      if (freed.find({u, v}) != freed.end()) will++;
      p[u] = v, dd[u] = dd[v] + 1, DFS2(u, will);
    }
}
void builder() {
  p.resize(n, -1);
  dd.resize(n, 0);
  cnt.resize(n, 0);
  p[0] = 0;
  dd[0] = 0;
  if (viv) cout << "st DFS2()" << endl;
  DFS2(0, 0);
  if (viv) {
    cout << "p = ";
    for (auto i : p) cout << i << ' ';
    cout << endl;
  }
  if (viv) cout << "end DFS2()" << endl;
  pp[0] = p;
  if (viv) cout << "start" << endl;
  for (int i = 1; i < K; i++) {
    if (viv) cout << "see: " << i << endl;
    pp[i].resize(n);
    for (int v = 0; v < n; v++) pp[i][v] = pp[i - 1][pp[i - 1][v]];
  }
  if (viv) cout << "built" << endl;
}
int LA(int v, int up) {
  if (viv) cout << "LA(" << v << ' ' << up << ")" << endl;
  for (int i = K - 1; i >= 0; i--)
    if (up >= (1 << i)) v = pp[i][v], up -= (1 << i);
  return v;
}
int LCA(int u, int v) {
  if (viv) cout << "LCA(" << u << ' ' << v << ")" << endl;
  if (d[u] >= d[v]) u = LA(u, d[u] - d[v]);
  if (d[v] >= d[u]) v = LA(v, d[v] - d[u]);
  if (u == v) return v;
  for (int i = K - 1; i >= 0; i--) {
    int pu = pp[i][u];
    int pv = pp[i][v];
    if (pu != pv) u = pu, v = pv;
  }
  return p[u];
}
int count(int u, int v) {
  if (viv) cout << "count(" << u << ' ' << v << ")" << endl;
  return cnt[u] + cnt[v] - cnt[LCA(u, v)] * 2;
}
void solve() {
  cin >> n >> m;
  e.resize(n);
  E.resize(n);
  es.resize(n);
  X = 0;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    e[a].push_back({b, c});
    e[b].push_back({a, c});
    es[a].insert(b);
    es[b].insert(a);
    X ^= c;
  }
  zero = (long long)n * (n - 1) / 2 - m;
  if (viv) {
    cout << "X = " << X << endl;
    cout << "zero = " << zero << endl;
  }
  for (int i = 0; i < n; i++) unused.push_back(i);
  d.resize(n, -1);
  int nc = 0;
  for (int i = 0; i < n; i++)
    if (d[i] == -1) DFS(i, nc), nc++;
  if (zero != n - nc) nice = true;
  long long MN = X;
  if (nice) MN = 0;
  if (viv) {
    cout << "d = ";
    for (auto i : d) cout << i << ' ';
    cout << '\n';
    cout << "nice = " << nice << endl;
    cout << "MN = " << MN << endl;
    cout << "n = " << n << endl;
  }
  for (int i = 0; i < n; i++) {
    for (auto p : e[i]) {
      long long v = d[i];
      long long u = d[p.first];
      if (v == u)
        MN = min(MN, (long long)p.second);
      else {
        all.push_back({p.second, {i, p.first}});
      }
    }
  }
  if (viv) {
    cout << "TOTAL MN = " << MN << endl;
    cout << "n = " << n << endl;
  }
  sort(all.begin(), all.end());
  DSU dsu(nc);
  long long ans = 0;
  vector<pair<long long, pair<long long, long long>>> rest;
  for (auto pr : all) {
    long long w = pr.first;
    int u = d[pr.second.first];
    int v = d[pr.second.second];
    if (dsu.uni(u, v)) {
      E[pr.second.first].push_back(pr.second.second);
      E[pr.second.second].push_back(pr.second.first);
      ans += w;
    } else
      rest.push_back(pr);
  }
  if (viv) {
    cout << "E" << ' ' << n << ":" << endl;
    for (int v = 0; v < n; v++) {
      cout << "\t" << v << " - ";
      for (auto u : E[v]) cout << u << ' ';
      cout << endl;
    }
  }
  builder();
  for (auto pr : rest) {
    if (count(pr.second.first, pr.second.second)) MN = min(MN, pr.first);
  }
  ans += MN;
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
