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>
#pragma GCC optimize ("O2,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;

//g++-10 -Wl,-stack_size -Wl,100000000 main.cpp -o test1
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//#define INFF 9223372036854775807
#define INF 1000000000000000001

#define debug1 cout<<"helo 1"<<endl;
#define debug2 cout<<"helo 2"<<endl;
#define endl '\n'
#define couti(a) printf("%d ",a)
#define coutl(a) printf("%lld ",a)
#define nline printf("\n")
#define cini(a) scanf("%d",&a)
#define cinl(a) scanf("%lld",&a)

#define mp make_pair
#define f first
#define pb push_back
#define pob pop_back
#define s second
#define t third
#define all(v) v.begin(),v.end()
#define all1(v) v.begin()+1,v.end()

//#define int long long
#define ll long long
#define LL long long

//#define PI 3.14159265358979323846264338327950288419716939937510582097

#define priorqmax priority_queue<LL>
#define priorqmin priority_queue<LL,vector<LL>,greater<LL> >
#define len length()
#define vi vector<int>
#define vl vector<ll>
#define ve vector
#define lsti list<int>
#define pairi pair<int,int>
#define pairl pair<ll,ll>
#define mset(a) memset(a,0,sizeof(a))
#define FOR(j, a, n) for(int j=a;j<n;++j)
#define FOR1(j, a, n) for(int j=a;j>=n;--j)

//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

class trio {
 public:
	int first, second, third;

	void print() {
		cout << first << " " << second << " " << third << endl;
	}
};

//#define MOD 1000000007
//#define MOD 987654319
#define MOD 998244353

bool comp_trio(trio &a, trio &b) {
	if (a.t == b.t) {
		if (a.s == b.s)
			return a.f < b.f;
		return a.s < b.s;
	}
	return a.t < b.t;
}

class compare {
 public:
	bool operator()(const pairi &a, const pairi &b) const {
		if (a.s == b.s)
			return a.f < b.f;
		return a.s < b.s;
	}
};

LL power(LL x, LL y, LL m) {

	if (y < 0)
		return 0;

	//(x^y)%MOD
	LL ans = 1, s = x;
	while (y) {
		if (y & 1) {
			ans *= s;
			ans %= m;
		}
		s *= s;
		s %= m;
		y >>= 1;
	}
	return ans;
}


// .................................................. START
// Check if int needs to be converted to long long  _/\_
//

class mkDSU {
 public:
	vector<int> par, size;

	mkDSU(int n) {
		par.resize(n);
		size.resize(n);

		FOR(i, 0, n) {
			par[i] = i;
			size[i] = 0;
		}
	}

	int find(int a) {
		int p = a, t;
		while (par[p] != p)
			p = par[p];

		while (p != a) {
			t = par[a];
			par[a] = p;
			a = t;
		}
		return a;
	}

	void union_(int u1, int u2) {

		if (size[u1] < size[u2]) {
			par[u1] = u2;
		} else {
			if (size[u2] < size[u1]) {
				par[u2] = u1;
			} else {
				par[u1] = u2;
				size[u2]++;
			}
		}
	}
};

ve<set<int>> adj;
set<int> ava;

void dfs(int i, mkDSU &dsu) {
	ava.erase(i);

	for (int j = 0;;) {

		auto it = ava.lower_bound(j);

		if (it == ava.end())
			return;

		j = *it;

		if (adj[i].find(j) == adj[i].end()) {
			int f1= dsu.find(i);
			int f2 = dsu.find(j);
			dsu.union_(f1, f2);
			dfs(j, dsu);
		}

		j++;

	}

}

void solve() {

	int n, m;
	cini(n);
	cini(m);

	adj.assign(n, set<int>());

	ve<trio> edges;

	int u, v, w;
	FOR(i, 0, m) {
		cini(u);
		cini(v);
		cini(w);
		u--;
		v--;
		adj[u].insert(v);
		adj[v].insert(u);
		edges.pb({u, v, w});
	}

	FOR(i, 0, n) ava.insert(i);

	int total_edges = (n * (n - 1)) / 2;

	bool forest = 0;

	if (total_edges - m <= n) {
		mkDSU dsu(n);

		forest = 1;

		FOR(i, 0, n) {
			FOR(j, i + 1, n) {
				if (adj[i].find(j) == adj[i].end()) {
					int f1= dsu.find(i);
					int f2 = dsu.find(j);
					if (f1 == f2) {
						forest = 0;
						break;
					}
					dsu.union_(f1, f2);
				}
			}

			if (!forest)
				break;
		}

	}

	if (forest) {

		int x = 0;

		FOR(i, 0, m) {
			x ^= edges[i].t;
		}

		int rem = 0;

		FOR(i, 0, n) {
			FOR(j, i + 1, n) {
				if (adj[i].find(j) == adj[i].end()) {
					edges.pb({i, j, 0});
					rem++;
				}
			}
		}

		sort(all(edges), comp_trio);


		ll ans = INF;

		FOR(i, 0, rem) {

			ll cur = 0;

			mkDSU dsu(n);

			bool fl=1;

			for (int j=0;j<edges.size();) {

				if(i==j){
					j++;
					continue;
				}

				if(fl  && x<edges[j].t){
					fl=0;
					int f1= dsu.find(edges[i].f);
					int f2 = dsu.find(edges[i].s);
					if (f1!= f2) {
						cur += x;
						dsu.union_(f1,f2);
					}

				}
				else{
					int f1= dsu.find(edges[j].f);
					int f2 = dsu.find(edges[j].s);
					if (f1!= f2) {
						cur += edges[j].t;
						dsu.union_(f1,f2);
					}
					j++;
				}


			}

			if(fl){
				int f1= dsu.find(edges[i].f);
				int f2 = dsu.find(edges[i].s);
				if (f1!= f2) {
					cur += x;
					dsu.union_(f1,f2);
				}

			}

			ans = min(cur, ans);

		}

		coutl(ans);

	} else {
		mkDSU dsu(n);

		FOR(i, 0, n) {
			if (ava.find(i) != ava.end())
				dfs(i, dsu);
		}

		sort(all(edges), comp_trio);

		ll ans = 0;

		for (auto it:edges) {
			int f1= dsu.find(it.f);
			int f2 = dsu.find(it.s);
			if (f1 != f2) {
				ans += it.t;
				dsu.union_(f1, f2);
			}
		}

		coutl(ans);

	}

}

int32_t main() {

//	fastio;

	int T = 1;
//	cin >> T;

	while (T--) {
		solve();
	}
}

