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;
const int mod = 1000000007;
const long long oo = 1e18;
const long double eps = 1e-8;
long long fans = 0;
long long n, m, block, sum = 0, use0 = 0;
long long rt[200020], id[200020];
long long get_r(int x) { return rt[x] == x ? x : rt[x] = get_r(rt[x]); }
vector<pair<int, int> > edges;
struct e {
  long long st, ed, v;
  e() {}
  e(int stt, int edd, int vv) { st = stt, ed = edd, v = vv; }
};
vector<e> ed;
bool ord(const e &a, const e &b) { return a.v < b.v; }
long long rt2[200020];
long long get_r2(int x) { return rt2[x] == x ? x : rt2[x] = get_r(rt2[x]); }
map<pair<int, int>, bool> mp;
vector<int> leaders;
int siz[5001];
int cnt[5001][5001];
vector<e> cand;
vector<pair<int, int> > ban;
long long search(int b) {
  for (int i = 1; i <= n; i++) rt[i] = i;
  int num = n;
  long long pans = 0;
  for (int i = 0; num > 1 && i < cand.size(); i++) {
    if (i == b) continue;
    int x = get_r(cand[i].st);
    int y = get_r(cand[i].ed);
    int v = cand[i].v;
    if (x != y) {
      rt[x] = y;
      num--;
      pans += v;
    }
  }
  return pans;
}
int main() {
  srand(time(0));
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) rt[i] = rt2[i] = i;
  for (int a, b, val, i = 1; i <= m; i++) {
    cin >> a >> b >> val;
    sum ^= val;
    edges.push_back(make_pair(a, b));
    edges.push_back(make_pair(b, a));
    ed.push_back(e(a, b, val));
    mp[make_pair(a, b)] = mp[make_pair(b, a)] = true;
  }
  sort(edges.begin(), edges.end());
  block = n;
  while (block >= 5000) {
    int x = rand() % n + 1;
    for (int i = 1; i <= n; i++)
      if (get_r(i) != get_r(x) && !mp[make_pair(x, i)]) {
        block--;
        rt[rt[i]] = rt[x];
      }
  }
  for (int i = 1; i <= n; i++)
    if (get_r(i) == i) {
      id[i] = leaders.size();
      leaders.push_back(i);
    }
  for (int i = 1; i <= n; i++) siz[id[get_r(i)]]++;
  for (int i = 0; i < ed.size(); i++) {
    int x = id[get_r(ed[i].st)];
    int y = id[get_r(ed[i].ed)];
    cnt[x][y]++, cnt[y][x]++;
  }
  for (int i = 0; i < leaders.size(); i++)
    for (int j = i + 1; j < leaders.size(); j++) {
      get_r(leaders[i]);
      get_r(leaders[j]);
      if (rt[leaders[i]] == rt[leaders[j]]) continue;
      if (cnt[i][j] / siz[i] < siz[j]) {
        rt[rt[leaders[j]]] = rt[leaders[i]];
        block--;
      }
    }
  use0 = n - block;
  sort(ed.begin(), ed.end(), ord);
  int pt = 0;
  for (; pt < ed.size() && block > 1; pt++) {
    int x = get_r(ed[pt].st);
    int y = get_r(ed[pt].ed);
    int v = ed[pt].v;
    if (get_r2(x) == get_r2(y)) continue;
    rt2[x] = rt2[y];
    block--;
    fans += v;
  }
  if (use0 < n * (n - 1) / 2 - m) {
    cout << fans << endl;
    return 0;
  }
  long long ans = fans + sum;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (!mp[make_pair(i, j)]) {
        ban.push_back(make_pair(i, j));
        cand.push_back(e(i, j, 0));
      }
  for (int i = 0; i < ed.size(); i++) cand.push_back(ed[i]);
  for (int i = 0; i < ban.size(); i++) {
    long long pans = search(i);
    ans = min(ans, pans);
  }
  cout << ans << endl;
  return 0;
}
