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>
#define cs const
#define pb push_back
using namespace std;

typedef long long ll;

cs int N = 2e5 + 5; 

int n, m, all;
unordered_map<int, int> e[N];
int pre[N], nxt[N], ex[N];
int fa[N], anc[N];
int fnd(int x) {
	if(x == fa[x]) return x; 
	return fa[x] = fnd(fa[x]);
}
int Fnd(int x) {
	if(x == anc[x]) return x; 
	return anc[x] = Fnd(anc[x]);
}
struct E{
	int u, v, w; 
	bool operator < (cs E &z) cs {
		return w < z.w; 
	}
} rm[N];

void bfs(int u) {
	static int frm[N];
	queue<int> q; 
	q.push(u);
	while(!q.empty()) {
		int x = q.front(); q.pop();
		for(int v = nxt[0]; v <= n; v = nxt[v])
		if(v != x && v != frm[x] && !e[x][v]) {
			++ ex[v];
			fa[fnd(v)] = fnd(x);
			if(ex[v] == 2) {
				all = 0; 
				pre[nxt[v]] = pre[v];
				nxt[pre[v]] = nxt[v];
			}
			else frm[v] = x, q.push(v);
		}
	}

}

int main() {
	#ifdef FSYo
	freopen("1.in", "r", stdin);
	#endif
	cin >> n >> m; 
	for(int i = 1, u, v, w; i <= m; i++) {
		scanf("%d%d%d", &u, &v, &w);
		e[u][v] = e[v][u] = 1; 
		all ^= w; 
		rm[i] = (E){u, v, w};
	}
	sort(rm + 1, rm + m + 1);
	for(int i = 1; i <= n; i++) 
		pre[i] = i - 1, nxt[i] = i + 1, fa[i] = anc[i] = i; 
	nxt[0] = 1, pre[n + 1] = n; 
	for(int i = 1; i <= n; i++)
	if(!ex[i]) bfs(i);
	
	ll ans = 0;
	for(int i = 1; i <= m; i++) {
		int x = fnd(rm[i].u);
		int y = fnd(rm[i].v);
		if(x == y) {
			if(Fnd(rm[i].u) != Fnd(rm[i].v)) {
				all = min(all, rm[i].w);
			} 
		}
		if(x != y) {
			ans += rm[i].w;
			fa[x] = y, anc[Fnd(rm[i].u)] = Fnd(rm[i].v);
		}
	}
	cout << ans + all;
	return 0;
}