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

/*
bts songs to dance to:
I need U
Run
ON
Filter
I'm fine
 */
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;

public class x1508C
{
    static final long CONS = 696969L;
    public static void main(String hi[]) throws Exception
    {
        FastScanner infile = new FastScanner();
        int N = infile.nextInt();
        int M = infile.nextInt();
        long xorTotal = 0;
        Edge[] edges = new Edge[M];
        HashSet<Long> setEdges = new HashSet<Long>(); //for 0 edge component checking
        int[] degree = new int[N];
        for(int i=0; i < M; i++)
        {
            int a = infile.nextInt()-1;
            int b = infile.nextInt()-1;
            if(a > b)
                a = (a+b)-(b=a);
            int w = infile.nextInt();
            //edges[a].add(new Edge(b, w));
            //edges[b].add(new Edge(a, w));
            edges[i] = new Edge(a, b, w);
            setEdges.add(CONS*a+b);
            xorTotal ^= (long)w;
            degree[a]++;
            degree[b]++;
        }
        //connect components with 0-edges
        LinkedList<Integer> active = new LinkedList<Integer>();
        for(int v=0; v < N; v++)
            active.add(v);
        int[] comp = new int[N];
        Arrays.fill(comp, -1);
        int boof = 0;
        //lmao
        ArrayList<Integer> choices = new ArrayList<Integer>();
        for(int v=0; v < N; v++)
            choices.add(v);
        Collections.sort(choices, (a,b) -> {
            return degree[a]-degree[b];
        });
        for(int v: choices)
            if(comp[v] == -1)
            {
                comp[v] = boof;
                ArrayDeque<Integer> q = new ArrayDeque<Integer>();
                q.add(v);
                while(q.size() > 0)
                {
                    int curr = q.poll();
                    ListIterator<Integer> pointer = active.listIterator();
                    while(pointer.hasNext())
                    {
                        int next = pointer.next();
                        if(next == v)
                        {
                            pointer.remove();
                            continue;
                        }
                        long key = CONS*min(curr, next)+max(curr, next);
                        if(!setEdges.contains(key))
                        {
                            q.add(next);
                            comp[next] = boof;
                            pointer.remove();
                        }
                    }
                }
                boof++;
            }
        DSU union = new DSU(N);
        int[] prev = new int[boof];
        Arrays.fill(prev, -1);
        for(int v=0; v < N; v++)
        {
            if(prev[comp[v]] != -1)
                union.merge(prev[comp[v]], v);
            prev[comp[v]] = v;
        }
        Arrays.sort(edges);
        long res = 0L;
        ArrayList<Edge> important = new ArrayList<Edge>();
        for(Edge e: edges)
            if(union.find(e.from) != union.find(e.to))
            {
                res += e.weight;
                union.merge(e.from, e.to);
                important.add(e);
            }
        //count dual edges
        long zeroCnt = (long)N*(N-1)/2-M;
        if(zeroCnt == N-boof)
        {
            union = new DSU(N);
            for(Edge e: important)
                union.merge(e.from, e.to);
            long best = xorTotal;
            for(Edge e: edges)
                if(union.find(e.from) != union.find(e.to))
                    best = min(best, e.weight);
            res += best;
        }
        System.out.println(res);
    }
}
class Edge implements Comparable<Edge>
{
    public int from;
    public int to;
    public int weight;

    public Edge(int s, int a, int b)
    {
        from = s;
        to = a;
        weight = b;
    }
    public int compareTo(Edge oth)
    {
        if(weight == oth.weight)
            return 0;
        else if(weight < oth.weight)
            return -1;
        return 1;
    }
}
class DSU
{
    public int[] dsu;

    public DSU(int N)
    {
        dsu = new int[N+1];
        for(int i=0; i <= N; i++)
            dsu[i] = i;
    }
    public int find(int x)
    {
        return dsu[x] == x ? x : (dsu[x] = find(dsu[x]));
    }
    public void merge(int x, int y)
    {
        int fx = find(x);
        int fy = find(y);
        dsu[fx] = fy;
    }
}
class FastScanner
{
    private int BS = 1 << 16;
    private char NC = (char) 0;
    private byte[] buf = new byte[BS];
    private int bId = 0, size = 0;
    private char c = NC;
    private double cnt = 1;
    private BufferedInputStream in;

    public FastScanner() {
        in = new BufferedInputStream(System.in, BS);
    }

    public FastScanner(String s) {
        try {
            in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
        } catch (Exception e) {
            in = new BufferedInputStream(System.in, BS);
        }
    }

    private char getChar() {
        while (bId == size) {
            try {
                size = in.read(buf);
            } catch (Exception e) {
                return NC;
            }
            if (size == -1) return NC;
            bId = 0;
        }
        return (char) buf[bId++];
    }

    public int nextInt() {
        return (int) nextLong();
    }

    public int[] nextInts(int N) {
        int[] res = new int[N];
        for (int i = 0; i < N; i++) {
            res[i] = (int) nextLong();
        }
        return res;
    }

    public long[] nextLongs(int N) {
        long[] res = new long[N];
        for (int i = 0; i < N; i++) {
            res[i] = nextLong();
        }
        return res;
    }

    public long nextLong() {
        cnt = 1;
        boolean neg = false;
        if (c == NC) c = getChar();
        for (; (c < '0' || c > '9'); c = getChar()) {
            if (c == '-') neg = true;
        }
        long res = 0;
        for (; c >= '0' && c <= '9'; c = getChar()) {
            res = (res << 3) + (res << 1) + c - '0';
            cnt *= 10;
        }
        return neg ? -res : res;
    }

    public double nextDouble() {
        double cur = nextLong();
        return c != '.' ? cur : cur + nextLong() / cnt;
    }

    public double[] nextDoubles(int N) {
        double[] res = new double[N];
        for (int i = 0; i < N; i++) {
            res[i] = nextDouble();
        }
        return res;
    }

    public String next() {
        StringBuilder res = new StringBuilder();
        while (c <= 32) c = getChar();
        while (c > 32) {
            res.append(c);
            c = getChar();
        }
        return res.toString();
    }

    public String nextLine() {
        StringBuilder res = new StringBuilder();
        while (c <= 32) c = getChar();
        while (c != '\n') {
            res.append(c);
            c = getChar();
        }
        return res.toString();
    }

    public boolean hasNext() {
        if (c > 32) return true;
        while (true) {
            c = getChar();
            if (c == NC) return false;
            else if (c > 32) return true;
        }
    }
}
