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;
int f[200005];
int Find(int first) {
  if (f[first] == first) return first;
  return f[first] = Find(f[first]);
}
vector<int> vv[200005];
vector<pair<int, int> > vvv[200005];
int ok[1005][1005];
void dfs(int first, int s, int f, int sum) {
  ok[s][first] = sum;
  for (auto it : vvv[first]) {
    if (it.first != f) {
      dfs(it.first, s, first, sum | it.second);
    }
  }
}
void solve() {
  long long n, m;
  scanf("%lld %lld", &n, &m);
  if (n * (n - 1) / 2 - m >= n) {
    vector<pair<int, pair<int, int> > > v;
    for (int i = 0; i < m; i++) {
      int first, second, w;
      scanf("%d %d %d", &first, &second, &w);
      v.push_back(make_pair(w, make_pair(first, second)));
      vv[first].push_back(second);
      vv[second].push_back(first);
    }
    set<int> s;
    for (int i = 1; i <= n; i++) {
      s.insert(i), f[i] = i;
      sort(vv[i].begin(), vv[i].end());
    }
    while (s.size()) {
      queue<int> q;
      q.push(*s.begin());
      s.erase(s.begin());
      while (!q.empty()) {
        int first = q.front();
        q.pop();
        int now = 0;
        vector<int> tmp;
        for (auto it : s) {
          while (now != vv[first].size() && vv[first][now] < it) {
            now++;
          }
          if (vv[first].size() != now && vv[first][now] == it) {
            now++;
            continue;
          }
          q.push(it);
          int a = Find(it);
          int b = Find(first);
          f[a] = b;
          tmp.push_back(it);
        }
        for (auto it : tmp) {
          s.erase(it);
        }
      }
    }
    sort(v.begin(), v.end());
    long long sum = 0;
    for (auto it : v) {
      int a = Find(it.second.first), b = Find(it.second.second);
      if (a != b) {
        sum += it.first;
        f[a] = b;
      }
    }
    printf("%lld\n", sum);
  } else {
    int vis[1005][1005];
    memset(vis, 0, sizeof(vis));
    vector<pair<int, pair<int, int> > > v;
    long long tot = 0;
    for (int i = 0; i < m; i++) {
      int first, second, w;
      scanf("%d %d %d", &first, &second, &w);
      v.push_back(make_pair(w, make_pair(first, second)));
      vis[first][second] = vis[second][first] = 1;
      tot ^= w;
    }
    sort(v.begin(), v.end());
    for (int i = 1; i <= n; i++) f[i] = i;
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (!vis[i][j]) {
          int a = Find(i), b = Find(j);
          vvv[i].push_back(make_pair(j, 1));
          vvv[j].push_back(make_pair(i, 1));
          if (a == b)
            tot = 0;
          else {
            f[a] = b;
          }
        }
      }
    }
    long long sum = tot;
    for (auto it : v) {
      int a = Find(it.second.first), b = Find(it.second.second);
      if (a != b) {
        f[a] = b;
        sum += it.first;
        vvv[a].push_back(make_pair(b, 0));
        vvv[b].push_back(make_pair(a, 0));
      }
    }
    for (int i = 1; i <= n; i++) dfs(i, i, i, 0);
    for (auto it : v) {
      int a = it.second.first, b = it.second.second;
      if (ok[a][b] && tot > it.first) {
        sum -= tot;
        sum += it.first;
        break;
      }
    }
    printf("%lld\n", sum);
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
