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>
const long long P = 998244353;
using namespace std;
template <typename T>
inline void ckmax(T &a, T b) {
  (a < b) && (a = b);
}
template <typename T>
inline void ckmin(T &a, T b) {
  (a > b) && (a = b);
}
inline long long mul(long long a, long long b) { return 1ll * a * b % P; }
inline long long add(long long a, long long b) {
  return a + b >= P ? a + b - P : a + b;
}
inline long long sub(long long a, long long b) {
  return a - b >= 0 ? a - b : a - b + P;
}
inline void mulmod(long long &a, long long b) { a = mul(a, b); }
inline void addmod(long long &a, long long b) { ((a += b) >= P) && (a -= P); }
inline void submod(long long &a, long long b) { ((a -= b) < 0) && (a += P); }
inline long long ksm(long long a, long long b) {
  long long ans = 1;
  for (; b; b >>= 1) {
    if (b & 1) ans = 1ll * ans * a % P;
    a = 1ll * a * a % P;
  }
  return ans;
}
inline void fprint(const vector<long long> &f) {
  for (long long i = 0; i < (long long)f.size(); i++)
    fprintf(stderr, "%d ", f[i]);
  fprintf(stderr, "\n");
}
inline long long inv(long long a) { return ksm(a, P - 2); }
namespace FastIO {
const long long SIZE = 1 << 16;
char buf[SIZE], obuf[SIZE], str[64];
long long bi = SIZE, bn = SIZE, opt;
long long read(char *s) {
  while (bn) {
    for (; bi < bn && buf[bi] <= ' '; bi++)
      ;
    if (bi < bn) break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  long long sn = 0;
  while (bn) {
    for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
    if (bi < bn) break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  s[sn] = 0;
  return sn;
}
bool read(long long &x) {
  if (x) x = 0;
  long long bf = 0, n = read(str);
  if (!n) return 0;
  long long i = 0;
  if (str[i] == '-') bf = 1, i = 1;
  for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
  if (bf) x = -x;
  return 1;
}
void write(long long x) {
  if (!x)
    obuf[opt++] = '0';
  else {
    if (x < 0) obuf[opt++] = '-', x = -x;
    long long sn = 0;
    while (x) str[sn++] = x % 10 + '0', x /= 10;
    for (long long i = sn - 1; i >= 0; i--) obuf[opt++] = str[i];
  }
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void write(char x) {
  obuf[opt++] = x;
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void Fflush() {
  if (opt) fwrite(obuf, 1, opt, stdout);
  opt = 0;
}
}  // namespace FastIO
inline long long read() {
  long long x;
  FastIO::read(x);
  return x;
}
const long long MN = 2e5 + 5;
long long n, m;
struct ele {
  long long u, v, w;
  bool operator<(const ele &b) const { return w < b.w; }
} g[MN];
vector<pair<long long, long long> > e[MN];
set<long long> lft;
struct dsu {
  static const long long N = MN;
  long long f[N];
  dsu() {
    for (long long i = (0); i <= (N - 1); i++) f[i] = i;
  }
  long long find(long long u) { return (u == f[u]) ? u : f[u] = find(f[u]); }
  long long merge(long long u, long long v) {
    if ((u = find(u)) == (v = find(v))) return 0;
    f[u] = v;
    return 1;
  }
} d;
long long col[MN], tot;
long long ex;
void dfs(long long u) {
  lft.erase(u);
  col[u] = tot;
  for (long long v = 0;;) {
    do {
      v++;
      v = *lft.lower_bound(v);
      if (v == n + 1) return;
    } while (
        (lower_bound(e[u].begin(), e[u].end(), make_pair(v, 0ll))->first) == v);
    ex--;
    dfs(v);
  }
}
signed main() {
  n = read(), m = read();
  ex = 1ll * n * (n - 1) / 2 - m;
  for (long long i = (1); i <= (n); i++) {
    lft.insert(i);
  }
  lft.insert(n + 1);
  long long x = 0;
  for (long long i = (1); i <= (m); i++) {
    long long u = read(), v = read(), w = read();
    x ^= w;
    g[i] = (ele){u, v, w};
    e[u].push_back(make_pair(v, w)), e[v].push_back(make_pair(u, w));
  }
  for (long long u = (1); u <= (n); u++) {
    sort(e[u].begin(), e[u].end());
  }
  sort(g + 1, g + 1 + m);
  for (long long u = (1); u <= (n); u++) {
    e[u].push_back(make_pair(n + 2, 0));
  }
  for (long long i = (1); i <= (n); i++) {
    if (!col[i]) {
      tot++;
      dfs(i);
    }
  }
  assert(ex >= 0);
  ex = (ex == 0);
  long long ans = 0;
  for (long long i = (1); i <= (m); i++) {
    long long u = g[i].u, v = g[i].v, w = g[i].w;
    if (d.merge(col[u], col[v]))
      ans += w;
    else
      ckmin(x, w);
  }
  cerr << ans << ' ' << ex << endl;
  cout << ans + ex * x << endl;
  return FastIO::Fflush(), 0;
}
