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 dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const long long linf = 4000000000000000000LL;
const long long inf = 1000000007;
void pv(vector<int> a) {
  for (auto& x : a) cout << x << " ";
  cout << '\n';
}
void pv(vector<long long> a) {
  for (auto& x : a) cout << x << " ";
  cout << '\n';
}
void pv(vector<vector<int>> a) {
  for (int i = (0); i < (int(a.size())); ++i) {
    cout << i << '\n';
    pv(a[i]);
    cout << '\n';
  }
}
void pv(vector<vector<long long>> a) {
  for (int i = (0); i < (int(a.size())); ++i) {
    cout << i << '\n';
    pv(a[i]);
  }
  cout << '\n';
}
void pv(vector<string> a) {
  for (auto& x : a) cout << x << '\n';
  cout << '\n';
}
void setIO(string second) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (int(second.size())) {
    freopen((second + ".in").c_str(), "r", stdin);
    if (second != "test1") freopen((second + ".out").c_str(), "w", stdout);
  }
}
int n, m;
const int MAX = 5005;
int link[MAX];
bitset<MAX> adj[MAX];
int find(int i) { return i == link[i] ? i : link[i] = find(link[i]); }
int unite(int i, int j) {
  i = find(i), j = find(j);
  if (i != j) {
    link[i] = j;
    return 1;
  }
  return 0;
}
int main() {
  setIO("");
  cin >> n >> m;
  if (n >= MAX) {
    cout << 0 << '\n';
    return 0;
  }
  int c = 0;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      e;
  for (int i = (0); i < (m); ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    x--, y--;
    e.push({z, {x, y}});
    c ^= z;
    adj[x][y] = adj[y][x] = 1;
  }
  map<pair<int, int>, pair<int, int>> next;
  pair<int, int> last = {-1, -1}, start = {-1, -1};
  int d = 0;
  for (int i = (0); i < (n); ++i) {
    for (int j = (i + 1); j < (n); ++j) {
      if (!adj[i][j]) {
        d++;
        if (last.first == -1) {
          start = {i, j};
        }
        if (last.first != -1) next[last] = {i, j};
        last = {i, j};
      }
    }
  }
  iota(link, link + n, 0);
  next[last] = {-1, -1};
  e.push({0, start});
  long long ans = 0;
  int count = n - 1;
  while (int(e.size()) && count) {
    pair<int, pair<int, int>> t = e.top();
    e.pop();
    int i = t.second.first, j = t.second.second;
    if (i == -1) continue;
    if (unite(i, j)) {
      count--;
      ans += t.first;
      if (n == 633 && t.first) cout << t.first << '\n';
      if (!adj[i][j]) {
        pair<int, int> temp = next[{i, j}];
        i = temp.first, j = temp.second;
        d--;
        if (d == 1) {
          e.push({c, temp});
        } else {
          e.push({0, temp});
        }
      }
    } else {
      if (!adj[i][j]) {
        pair<int, int> temp = next[{i, j}];
        i = temp.first, j = temp.second;
        if (d == 1) {
          e.push({c, temp});
        } else {
          e.push({0, temp});
        }
      }
    }
  }
  cout << ans << '\n';
}
