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

public class C {
	static byte[] buf = new byte[1<<26];
    static int bp = -1;	
	
	public static void main(String[] args) throws IOException {

		/**/
		DataInputStream in = new DataInputStream(System.in);
		/*/
		DataInputStream in = new DataInputStream(new FileInputStream("src/c.in"));
		/**/
		
		in.read(buf, 0, 1<<26);
		
		int n = nni();
		int m = nni();
		int[] us = new int[m];
		int[] vs = new int[m];
		int[] ws = new int[m];
		HashMap<Long, Integer> g = new HashMap<>();
		PriorityQueue<Long> pq = new PriorityQueue<>();
		PriorityQueue<Long> pq2 = new PriorityQueue<>();
		
		int xor = 0;
		int te = (n<10000)?(n*(n-1)/2):-1;
		int used = 0;
		for (int i = 0; i < m; ++i) {
			int u = nni()-1;
			int v = nni()-1;
			int w = nni();
			us[i] = u;
			vs[i] = v;
			ws[i] = w;
			g.put(((long)u)*n+v, i);
			g.put(((long)v)*n+u, i);
			xor ^= w;
			pq.add((((long)w)<<32)+i);
			pq2.add((((long)w)<<32)+i);
		}
		
		if (n==2) {
			System.out.println(0);
			return;
		}
		
		int[] which = new int[n];
		HashSet<Integer> uw = new HashSet<>();
		ArrayList<ArrayList<Integer>> ch = new ArrayList<>();
		for (int i = 0; i < n; ++i) {
			ch.add(new ArrayList<>());
			which[i] = i;
			ch.get(i).add(i);
			uw.add(i);
		}
		for (int i = 0; i < n; ++i) {
			if (which[i]!=i)
				continue;
			uw.remove(i);
			ArrayDeque<Integer> q = new ArrayDeque<>();
			q.add(i);
			while (!q.isEmpty()) {
				int u = q.removeFirst();
				HashSet<Integer> uwr = new HashSet<>();
				for (int v : uw) {
					if (!g.containsKey(((long)u)*n+v)) {
						uwr.add(v);
						which[v] = i;
						ch.get(i).add(v);
						q.add(v);
						++used;
					}
				}
				uw.removeAll(uwr);
			}
		}
		long ugly = 0L;
		while (!pq.isEmpty()) {
			long pq0 = pq.remove();
			int w = (int)(pq0>>32);
			int e = (int)pq0;
			int u = which[us[e]];
			int v = which[vs[e]];
			if (u!=v) {
				ugly += w;
				if (ch.get(v).size()>ch.get(u).size()) {
					int t = v;
					v = u;
					u = t;
				}
				for (int v2 : ch.get(v)) {
					which[v2] = u;
					ch.get(u).add(v2);
				}
				ch.get(v).clear();
			}
		}
		
		if (used+m==te) {
			ugly += xor;
			
			long ugly2 = 0;
			int rem = n-1;
			which = new int[n];
			ch = new ArrayList<>();
			for (int i = 0; i < n; ++i) {
				ch.add(new ArrayList<>());
				which[i] = i;
				ch.get(i).add(i);
			}
			while (rem>=used) {
				long pq0 = pq2.remove();
				int w = (int)(pq0>>32);
				int e = (int)pq0;
				int u = which[us[e]];
				int v = which[vs[e]];
				if (u!=v) {
					ugly2 += w;
					if (ch.get(v).size()>ch.get(u).size()) {
						int t = v;
						v = u;
						u = t;
					}
					for (int v2 : ch.get(v)) {
						which[v2] = u;
						ch.get(u).add(v2);
					}
					ch.get(v).clear();
					--rem;
				}
			}
			ugly = Math.min(ugly, ugly2);
		}
		System.out.println(ugly);
	}
	
	public static int nni() {
        int ret = 0;
        byte b = buf[++bp];
        while (true) {
            ret = ret*10+b-'0';
            b = buf[++bp];
            if (b<'0'||b>'9') {
            	while (buf[bp+1]=='\r'||buf[bp+1]=='\n'||buf[bp+1]==' ') {++bp;}
            	break;
            }
        }
        return ret;
    }
}