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.util.*;
import java.io.*;

public class C1508 {
	static HashMap<Integer, Integer>[] adjList;

	public static long mst(int n, int m, int xor) {
		Queue<Integer> q = new LinkedList<Integer>();
		q.add(0);
		LinkedList<Integer> notVis = new LinkedList<Integer>();
		for (int i = 1; i < n; i++) {
			notVis.add(i);
		}
		PriorityQueue<int[]> edgePool = new PriorityQueue<int[]>((u, v) -> u[1] - v[1]);
		boolean[] inQueue = new boolean[n];
		inQueue[0] = true;
		int cntEdge = 0;
		long ans = 0;
		int cntZeroEdges = 0;
		while (cntEdge != n - 1) {
			while (!q.isEmpty()) {
				int cur = q.poll();
//				Stack<Integer> removed = new Stack<Integer>();
				Iterator<Integer> it = notVis.listIterator();
				while (it.hasNext()) {
					int x = it.next();
					Integer get = adjList[cur].get(x);
					if (get != null) {
						edgePool.add(new int[] { x, get, cur });
					} else {
//						removed.add(x);
						it.remove();
						if (!inQueue[x]) {
							cntEdge++;
							cntZeroEdges++;
							q.add(x);
							inQueue[x] = true;
						}
					}
				}
//				while (!removed.isEmpty()) {
//					notVis.remove(removed.pop());
//				}

			}
			if (cntEdge != n - 1) {
				while (!edgePool.isEmpty()) {
					int[] cur = edgePool.poll();
					if (!inQueue[cur[0]]) {
						q.add(cur[0]);
						inQueue[cur[0]] = true;
						ans += cur[1];
						cntEdge++;
						break;
					}
				}
			}
		}
		long remZeroEdges = 1l * n * (n - 1) / 2 - m - cntZeroEdges;
		if (remZeroEdges > 0) {
			return ans;
		}
		if (n >= 900)
			throw null;
		int[][] edgeList = new int[n * (n - 1) / 2][];
		int idx = 0;
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (adjList[i].containsKey(j)) {
					edgeList[idx++] = new int[] { i, j, adjList[i].get(j) };
				} else {
					edgeList[idx++] = new int[] { i, j, 0 };
				}
			}
		}
		Arrays.sort(edgeList, (u, v) -> u[2] - v[2]);
		ans = ans + xor;
		for (int[] forb : edgeList) {
			if (forb[2] == 0)
				ans = Math.min(ans, forbMST(forb, edgeList, n));
			else
				break;
		}
		return ans;
	}

	public static long forbMST(int[] forb, int[][] edgeList, int n) {
		UnionFind uf = new UnionFind(n);
		long mst = 0;
		for (int[] edge : edgeList) {
			if (edge == forb)
				continue;
			if (uf.numDisjointSets() == 1)
				break;
			if (!uf.isSameSet(edge[0], edge[1])) {
				uf.unionSet(edge[0], edge[1]);
				mst += edge[2];
			}
		}
		if (uf.numDisjointSets() != 1) {
			return (long) 2e18;
		}
		return mst;
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		int m = sc.nextInt();
		adjList = new HashMap[n];
		for (int i = 0; i < adjList.length; i++) {
			adjList[i] = new HashMap<Integer, Integer>();
		}
		int xor = 0;
		for (int i = 0; i < m; i++) {
			int u = sc.nextInt() - 1;
			int v = sc.nextInt() - 1;
			int w = sc.nextInt();
			adjList[u].put(v, w);
			adjList[v].put(u, w);
			xor ^= w;
		}
		pw.println(mst(n, m, xor));
		pw.close();
	}

	static class UnionFind {
		int[] p, rank, setSize;
		int numSets;

		public UnionFind(int N) {
			p = new int[numSets = N];
			rank = new int[N];
			setSize = new int[N];
			for (int i = 0; i < N; i++) {
				p[i] = i;
				setSize[i] = 1;
			}
		}

		public int findSet(int i) {
			return p[i] == i ? i : (p[i] = findSet(p[i]));
		}

		public boolean isSameSet(int i, int j) {
			return findSet(i) == findSet(j);
		}

		public void unionSet(int i, int j) {
			if (isSameSet(i, j))
				return;
			numSets--;
			int x = findSet(i), y = findSet(j);
			if (rank[x] > rank[y]) {
				p[y] = x;
				setSize[x] += setSize[y];
			} else {
				p[x] = y;
				setSize[y] += setSize[x];
				if (rank[x] == rank[y])
					rank[y]++;
			}
		}

		public int numDisjointSets() {
			return numSets;
		}

		public int sizeOfSet(int i) {
			return setSize[findSet(i)];
		}
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}
