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 T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 1e6 + 5;

struct dsu0 {
    vector<int> par, siz;
    int n;
    int cc;
    int largest;
    void init(int n) {
	assert(n>0);
	this->n=n;
	cc=n;
	par.resize(n+10);siz.resize(n+10);
	for (int i=0; i<n; i++) par[i]=i,siz[i]=1;
	largest=1;
    }
    int parent(int x) {
	assert(x>=0 && x<n);
	return par[x]==x?x:par[x]=parent(par[x]);
    }
    bool join(int x, int y) {
	x=parent(x);y=parent(y);
	if (x==y) return false;
	cc--;
	if (siz[x]<siz[y]) swap(x,y);
	siz[x]+=siz[y];par[y]=x;
	largest=max(largest,siz[x]);
	return true;
    }
};


vector<array<int,3>> edges;
int n, m;
vector<int> g[maxn];

set<int> act;

dsu0 gp, gg;

ll avail;

void dfs(int at) {
    act.erase(at);
    for (int to=0;;) {
	auto it = act.lower_bound(to);
	if (it==act.end()) {
	    break;
	}
	to = *it;
	if (!binary_search(g[at].begin(), g[at].end(), to)) {
	    gp.join(at,to);
	    avail--;
	    dfs(to);
	}
	to++;
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    ll xum = 0;

    cin>>n>>m;
    for (int i=0; i<m; i++) {
	int u,v,w; cin>>u>>v>>w;
	--u; --v;
	g[u].push_back(v);
	g[v].push_back(u);
	edges.push_back({w,u,v});
	xum ^= w;
    }

    
    for (int i=0; i<n; i++) {
	sort(g[i].begin(), g[i].end());
    }

    avail = 1ll*n*(n-1)/2 - m;

    for (int i=0; i<n; i++) {
	act.insert(i);
    }
    
    gp.init(n);
    for (int i=0; i<n; i++) {
	if (act.count(i)) {
	    dfs(i);
	}
    }

    if (avail > 0) {
	xum = 0;
    }
    
    gg.init(n);
    sort(edges.begin(), edges.end());

    ll sum = 0;

    for (auto ed: edges) {
	ll w = ed[0];
	int u = ed[1];
	int v = ed[2];
	if (gp.join(u,v)) {
	    sum += w;
	    gg.join(u,v);
	} else if (gg.join(u,v)) {
	    xum = min(xum, w);
	}
    }

    sum += xum;
    out(sum);
    
    return 0;
}
