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;

#define dbg(x...) \
    do {          \
        cout << #x << " -> "; \
        err(x);              \
    } while (0)

void err() {
    cout << endl;
}

template <class T, class... Ts>
void err(const T &arg, const Ts &... args) {
    cout << arg << ' ';
    err(args...);
}

typedef long long ll;
typedef pair<int, int> pi;

const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

struct E {
    int u, v, w;
    bool operator < (const E &tmp) const {
        return w < tmp.w;
    }
};

struct Edge {
    int to, w;
};
vector<Edge> G[maxn];

int pre[maxn], nxt[maxn];
int n, m;

bool vis[maxn], inq[maxn];

void del(int x) {
    int u = pre[x], v = nxt[x];
    nxt[u] = v;
    pre[v] = u;
}

int fa[maxn];

int find(int x) {
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void init() {
    for (int i = 1; i <= n + 1; i++) {
        pre[i] = i - 1;
        nxt[i - 1] = i;
        fa[i] = i;
    }
    nxt[n] = n + 1;
    queue<int> q;
    while (nxt[0] <= n) {
        int now = nxt[0];
        del(now);
        q.push(now);
        inq[now] = true;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (Edge e : G[u]) {
                int v = e.to;
                vis[v] = true;
            }
            for (int i = nxt[0]; i <= n; i = nxt[i]) {
                if (!vis[i] && !inq[i]) {
                    inq[i] = true;
                    q.push(i);
                    fa[i] = now;
                    del(i);
                }
            }
            for (Edge e : G[u]) {
                int v = e.to;
                vis[v] = false;
            }
        }
    }
}

bool merge(int u, int v) {
    int fu = find(u), fv = find(v);
    if (fu != fv) {
        fa[fv] = fu;
    }
    return fv != fu;
}

void run() {
    scanf("%d%d", &n, &m);
    vector<E> edge;
    int extra = 0;
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        G[u].push_back({v, w});
        G[v].push_back({u, w});
        edge.push_back({u, v, w});
        extra ^= w;
    }
    ll rev_m = 1ll * n * (n - 1) / 2 - m;
//    dbg(extra);
    init();
    set<int> st;
    for (int i = 1; i <= n; i++) {
//        dbg(i, fa[i]);
        st.insert(fa[i]);
    }
    int used = n - st.size();
    if (rev_m > used) {
        extra = 0;
    }
    sort(edge.begin(), edge.end());
    ll ans = 0;
    vector<E> vec;
    for (E e : edge) {
        int u = e.u, v = e.v, w = e.w;
        if (merge(u, v)) {
            ans += w;
            vec.push_back(e);
        }
    }
    for (int i = 1; i <= n; i++) {
        fa[i] = i;
    }
    for (E e : vec) {
        merge(e.u, e.v);
    }
    for (E e : edge) {
        if (find(e.u) != find(e.v)) {
            extra = min(extra, e.w);
            break;
        }
    }
    ans += extra;
    printf("%lld\n", ans);
}

/*
740363158
2327197
738668314
*/

int main() {

    int T = 1;
//    scanf("%d", &T);
    while (T--) {
        run();
    }

    return 0;
}