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 <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define setIO(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
#define closefile fclose(stdin), fclose(stdout)
#define m_p make_pair
#define sz(x) (int)x.size()
#define see(x) cerr << x << " "
#define seeln(x) cerr << x << endl
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define outarr(x, l, r) {cerr<< #x"[" << l << " ~ " << r << "] = "; for (int _i = l; _i <= r; ++_i) cerr << x[_i] << " "; cerr << endl;}
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define gc() getchar()
//#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
template <class T> void read(T &x)
{
    x = 0; int c = gc(); int flag = 0;
    while (c < '0' || c > '9') flag |= (c == '-'), c = gc();
    while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) +(c ^ 48), c = gc();
    if (flag) x = -x;
}
template <class T> T _max(T a, T b){return b < a ? a : b;}
template <class T> T _min(T a, T b){return a < b ? a : b;}
template <class T> bool checkmax(T &a, T b){return a < b ? a = b, 1 : 0;}
template <class T> bool checkmin(T &a, T b){return b < a ? a = b, 1 : 0;}
const int N = 200005;
int n, m, Xor = 0;
int e = 0, first[N], nxt[N << 1], point[N << 1];
int pre[N], suf[N];
int cnt = 0, compo[N];
int vis[N], mark[N];
int cfa[N], fa[N];
void add_edge(int x, int y)
{
	nxt[++e] = first[x];
	first[x] = e;
	point[e] = y;
}

struct edge
{
	int u, v, w;
	edge(int u = 0, int v = 0, int w = 0):u(u), v(v), w(w){}
	bool operator < (const edge &rhs) const
	{
		return w < rhs.w;
	}
}E[N];
int ed = 0;

void remove(int x)
{
	pre[suf[x]] = pre[x];
	suf[pre[x]] = suf[x];
}

void bfs(int s)
{
	queue<int> q;
	q.push(s);
	vis[s] = 1;
	remove(s);
	while (!q.empty())
	{
		int x = q.front(); q.pop();
		compo[x] = cnt;
		for (int i = first[x]; ~i; i = nxt[i])
		{
			int to = point[i];
			mark[to] = 1;
		}
		for (int i = suf[0]; i; i = suf[i])
		{
			if (mark[i]) mark[i] = 0;
			else
			{
				vis[i] = 1;
				q.push(i);
				remove(i);
			}
		}
	}
}

void init()
{
	read(n); read(m);
	memset(first, -1, sizeof first);
	for (int i = 1, u, v, w; i <= m; ++i)
	{
		read(u); read(v); read(w);
		add_edge(u, v);
		add_edge(v, u);
		E[++ed] = edge(u, v, w);
		Xor ^= w;
	}
	sort(E + 1, E + ed + 1);
	for (int i = 1; i <= n; ++i) pre[i] = i - 1;
	for (int i = 0; i < n; ++i) suf[i] = i + 1;
	for (int i = 1; i <= n; ++i) if (!vis[i])
	{
		++cnt;
		bfs(i);
	}
}

int cgf(int x){return x == cfa[x] ? x : cfa[x] = cgf(cfa[x]);}
int gf(int x){return x == fa[x] ? x : fa[x] = gf(fa[x]);}
void solve()
{
	ll ans = 0; int F = 0;
	if (n - cnt == (ll)n * (n - 1) / 2 - m) ans = 0, F = 1;
	for (int i = 1; i <= n; ++i) fa[i] = i;
	for (int i = 1; i <= cnt; ++i) cfa[i] = i;
	int offset = Xor * F;
	for (int i = 1; i <= ed; ++i)
	{
		int u = E[i].u, v = E[i].v;
		int cu = cgf(compo[u]), cv = cgf(compo[v]);
		int gu = gf(u), gv = gf(v);
		if (cu ^ cv)
		{
			cfa[cu] = cv;
			int z1 = gf(u), z2 = gf(v);
			if (z1 ^ z2) fa[z1] = z2;
			ans += E[i].w;
		}
		else if (gu ^ gv)
		{
			fa[gu] = gv;
			checkmin(offset, E[i].w);
		}
	}
	ans += offset;
	printf("%lld\n", ans);
}

int main()
{
	//setIO("");
	init();
	solve();
	return 0;
}
