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

// Take 2, after reading editorial.
// I was surprised to see that my "small case" code was the case that was too slow! Alternative: find the cheapest way to "make a cycle."

#include <cassert>
#include <ctime>
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <set>


using namespace std;
typedef long long ll;
#define err(...) fprintf(stderr,__VA_ARGS__)
#define debug(exp) cerr << #exp << " = " << (exp) << " ";
#define myscanf(exp, num, ...) if (scanf(exp,__VA_ARGS__) != num) return false;

struct Edge {
    int src,tar, wt;
    bool operator<(const Edge &other) const {
        return wt < other.wt;
    }
    Edge(int s, int t, int w): src(s), tar(t), wt(w) {}
    Edge() {}
};

const int MAX = 200000;

/// Testcase variables
int n,m;
Edge edges[MAX];

struct dsu {
    int par[MAX+1];
    dsu() {
        fill(par,par+MAX+1,-1);
    }
    int anc(int i) {
        if (par[i] == -1) return i;
        else return par[i] = anc(par[i]);
    }
    bool join(int i, int j) {
        i = anc(i);
        j = anc(j);
        if (i == j) return false;
        par[i] = j;
        return true;
    }
} all, viaLab;

set<pair<int,int> > adjLab;
//vector<int> adj[MAX+1];
//set<int> seen;
//bool seen[MAX+1];
set<int> avail;
///

void unlabdfs(int i) {
    //seen.insert(i);
    
    if (avail.count(i) == 0) return;
    avail.erase(i);
    
    //if (seen[i]) return;
    //seen[i] = true;
    int j=0;
    //sort(adj[i].begin(),adj[i].end());
    while (true) {
        j++;
    //for (int j=1; j <= n; j++) {
     //   if (seen[j]) continue;
        auto it = avail.lower_bound(j);
        if (it == avail.end()) break;
        j = *it;
        if (adjLab.count(pair<int,int>(i,j))) continue;
        //if (binary_search(adj[i].begin(),adj[i].end(),j)) continue;
        all.join(i,j);
        unlabdfs(j);
    }
}

void solve_testcase() {
    int lastWt = 0;
    for (int e=0; e<m; e++) lastWt ^= edges[e].wt;

    //debug(lastWt); err("\n");

    avail.clear();
    for (int i=1; i<=n; i++) avail.insert(i);
    //for (int i=1; i<=n; i++) seen[i] = false;

    for (int i=1; i<=n; i++) unlabdfs(i);
    
    // Check for a cycle
    int components = 0;
    for (int i=1; i<=n; i++) {
        if (all.anc(i) == i) components++;
    }
    if ( (ll)n*(n-1)/2 - m > n - components) lastWt = 0;

    sort(edges, edges+m);
    ll score = 0;
    for (int e=0; e<m; e++) {
        //debug(edges[e].src); debug(edges[e].tar); debug(edges[e].wt); err("\n");
        if (all.join(edges[e].src, edges[e].tar)) {
            score += edges[e].wt;
            viaLab.join(edges[e].src, edges[e].tar);
        } else if (viaLab.join(edges[e].src, edges[e].tar)) {
            lastWt = min(lastWt, edges[e].wt);
            //debug(lastWt); err("\n");
        }
    }
    ll res = score + lastWt;
    cout << res << endl;
}

bool read_testcase() {
    cin >> n >> m;
    adjLab.clear();
    //for (int i=1; i<=n; i++) adj[i].clear();
    for (int i=0; i<m; i++) {
        scanf("%d %d %d",&edges[i].src, &edges[i].tar, &edges[i].wt);
        if (edges[i].src > edges[i].tar) swap(edges[i].src, edges[i].tar);
        //adj[edges[i].src].push_back(edges[i].tar);
        //adj[edges[i].tar].push_back(edges[i].src);
        adjLab.insert(pair<int,int>(edges[i].src, edges[i].tar));
        adjLab.insert(pair<int,int>(edges[i].tar,edges[i].src));
    }
    all = dsu();
    viaLab = dsu();
    return cin.good();
}

int main(int argc, char **argv) {
    if (argc>1 && argv[1][0]!= 'x') freopen(argv[1],"r",stdin);
    if (argc>2) freopen(argv[2],"w",stdout);
    int T=1;
    clock_t cur,prev = clock();
    while (read_testcase()) {
        err(">>>Solving testcase %d<<<<\n",T);
        solve_testcase();
        cur = clock();
        int dur = (cur-prev)*1000/CLOCKS_PER_SEC;
        err("____Case %d done in %dms.\n",T,dur);
        prev = cur; T++;
    }
}
