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;
template <typename elemType>
inline void Read(elemType& T) {
  elemType X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  T = (w ? -X : X);
}
struct DSU {
  int S[200010];
  int Rank[200010];
  void clear() {
    memset(S, 0, sizeof(S));
    memset(Rank, 0, sizeof(Rank));
  }
  int find(int u) {
    if (S[u] == 0)
      return u;
    else
      return S[u] = find(S[u]);
  }
  void merge_set(int u, int v) {
    if ((u = find(u)) == (v = find(v)))
      return;
    else if (Rank[u] > Rank[v])
      S[v] = u;
    else if (Rank[u] < Rank[v])
      S[u] = v;
    else {
      S[v] = u;
      Rank[u]++;
    }
  }
};
int belong[200010], sz[200010], ecnt[200010];
bool mark[200010];
DSU S;
int N, M, Index;
map<pair<int, int>, int> Map;
struct E {
  int u, v;
  long long w;
};
bool cmp(E A, E B) { return A.w < B.w; }
vector<E> edges;
void DFS(int u) {
  belong[u] = Index;
  ++sz[Index];
  for (int v = 1; v <= N; ++v) {
    if (v == u || belong[v] || Map.count(make_pair(u, v))) continue;
    DFS(v);
  }
}
inline long long Calc() {
  for (auto p : Map) {
    int u = belong[p.first.first], v = belong[p.first.second];
    long long w = p.second;
    if (u != v) edges.push_back((E){u, v, w});
  }
  sort(edges.begin(), edges.end(), cmp);
  int cnt = 0;
  long long res = 0;
  for (auto e : edges) {
    if (cnt == Index - 1) break;
    int u = S.find(e.u), v = S.find(e.v);
    if (u == v) continue;
    S.merge_set(u, v);
    res += e.w;
  }
  return res;
}
bool Judge() {
  if (N == 633) cout << Map.size() << endl;
  for (auto p : Map) {
    int u = belong[p.first.first], v = belong[p.first.second];
    if (u == v) ++ecnt[u];
  }
  for (int id = 1; id <= Index; ++id)
    if (sz[id] * (sz[id] - 1) / 2 - ecnt[id] / 2 > sz[id] - 1) return true;
  return false;
}
int main() {
  Read(N);
  Read(M);
  long long XorSum = 0;
  for (int i = 1; i <= M; ++i) {
    int u, v, w;
    Read(u);
    Read(v);
    Read(w);
    Map[make_pair(u, v)] = w;
    Map[make_pair(v, u)] = w;
    XorSum ^= w;
  }
  for (int u = 1; u <= N; ++u)
    if (!belong[u]) {
      ++Index;
      DFS(u);
    }
  long long Ans = Calc();
  bool flag = false;
  if (N == 633) cout << Ans << " !!!" << endl;
  if (!Judge()) Ans += XorSum;
  cout << Ans << endl;
  if (N == 633) cout << Index << " " << sz[1] << " " << ecnt[1] << endl;
  return 0;
}
