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

import java.io.*;
import java.util.*;

public class CF1509F extends PrintWriter {
	CF1509F() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF1509F o = new CF1509F(); o.main(); o.flush();
	}

	int n, m, x;
	int[] ii, jj, ww; Integer[] hh;

	int[] ds;
	int find(int i) {
		return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
	}
	boolean join(int i, int j) {
		i = find(i);
		j = find(j);
		if (i == j)
			return false;
		if (ds[i] > ds[j])
			ds[i] = j;
		else {
			if (ds[i] == ds[j])
				ds[i]--;
			ds[j] = i;
		}
		return true;
	}

	int[] eo; int[][] ej;
	void append(int i, int j) {
		int o = eo[i]++;
		if (o >= 2 && (o & o - 1) == 0)
			ej[i] = Arrays.copyOf(ej[i], o << 1);
		ej[i][o] = j;
	}
	TreeSet<Integer> ts;
	void dfs(int i) {
		Arrays.sort(ej[i], 0, eo[i]);
		for (int l = -1, o = 0; o <= eo[i]; o++) {
			int r = o < eo[i] ? ej[i][o] : n;
			Integer j;
			while ((j = ts.higher(l)) != null && j < r) {
				join(i, j);
				ts.remove(j);
				dfs(j);
			}
			l = r;
		}
	}

	long solve1() {
		eo = new int[n]; ej = new int[n][2];
		for (int h = 0; h < m; h++) {
			append(ii[h], jj[h]);
			append(jj[h], ii[h]);
		}
		ts = new TreeSet<>();
		for (int i = 0; i < n; i++)
			ts.add(i);
		for (int i = 0; i < n; i++)
			if (ts.remove(i))
				dfs(i);
		Arrays.sort(hh, (h1, h2) -> ww[h1] - ww[h2]);
		long ans = 0;
		for (int h = 0; h < m; h++) {
			int h_ = hh[h], i = ii[h_], j = jj[h_], w = ww[h_];
			if (join(i, j))
				ans += w;
		}
		return ans;
	}

	long solve2() {
		Arrays.sort(hh, (h1, h2) -> ii[h1] != ii[h2] ? ii[h1] - ii[h2] : jj[h1] - jj[h2]);
		boolean extra = false;
		for (int h = 0, i = 0; i < n; i++)
			for (int j = i + 1; j < n; j++) {
				if (h < m && i == ii[hh[h]] && j == jj[hh[h]]) {
					h++;
					continue;
				}
				if (!join(i, j))
					extra = true;
			}
		Arrays.sort(hh, (h1, h2) -> ww[h1] - ww[h2]);
		if (extra) {
			long ans = 0;
			for (int h = 0; h < m; h++) {
				int h_ = hh[h], i = ii[h_], j = jj[h_], w = ww[h_];
				if (join(i, j))
					ans += w;
			}
			return ans;
		}
		int[] ds_ = ds;
		ds = new int[n]; Arrays.fill(ds, -1);
		boolean[] skip = new boolean[m];
		for (int h = 0; h < m; h++) {
			int h_ = hh[h], i = ii[h_], j = jj[h_], w = ww[h_];
			if (w >= x)
				break;
			if (!join(i, j))
				skip[h] = true;
		}
		ds = ds_;
		long ans = 0;
		boolean replaced = false;
		for (int h = 0; h < m; h++) {
			if (skip[h])
				continue;
			int h_ = hh[h], i = ii[h_], j = jj[h_], w = ww[h_];
			if (join(i, j))
				ans += w;
			else if (w < x && !replaced) {
				ans += w;
				replaced = true;
			}
		}
		if (!replaced)
			ans += x;
		return ans;
	}

	void main() {
		n = sc.nextInt();
		m = sc.nextInt();
		ii = new int[m];
		jj = new int[m];
		ww = new int[m];
		hh = new Integer[m];
		for (int h = 0; h < m; h++) {
			int i = sc.nextInt() - 1;
			int j = sc.nextInt() - 1;
			int w = sc.nextInt();
			if (i > j) {
				int tmp = i; i = j; j = tmp;
			}
			x ^= w;
			ii[h] = i;
			jj[h] = j;
			ww[h] = w;
			hh[h] = h;
		}
		ds = new int[n]; Arrays.fill(ds, -1);
		long ans;
		if ((long) n * (n - 1) / 2 - m > n - 1)
			ans = solve1();	// ingore x
		else
			ans = solve2();	// n = O(sqrt(m))
		println(ans);
	}
}
