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.math.BigInteger;
import java.util.*;
 
public class Solution extends PrintWriter {

    int[] p;
    int[] sz;
    
    void make_set(int u) {
        p[u] = u;
        sz[u] = 1;
    }
    
    int find_set(int u) {
        if(u == p[u]) return u;
        else return p[u] = find_set(p[u]);
    }
    
    boolean union(int u, int v) {
        u = find_set(u);
        v = find_set(v);
        if(u == v) return false;
        if(sz[u] > sz[v]) {
            int temp = u;
            u = v;
            v = temp;
        }
        p[u] = v;
        sz[v] += sz[u];
        return true;
    }
    
    private void solve(){
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        
        long ans = 1L<<60;
        long rem = (n * (n - 1L)) / 2L - m;
        if(rem < n) {
            int xor = 0;
            int[][] adj = new int[n][n];
            for(int i = 0; i < m; i++) {
                int u = sc.nextInt()-1;
                int v = sc.nextInt()-1;
                int w = sc.nextInt();
                xor ^= w;
                adj[u][v] = w;
                adj[v][u] = w;
            }
            int m_ = (n*(n-1))/2;
            int[][] edges = new int[m_][];
            int pos = 0;
            for(int u = 0; u < n; u++) {
                for(int v = u+1; v < n; v++) {
                    edges[pos] = new int[] {u,v,adj[u][v], pos};
                    pos++;
                }
            }
            int bad = 0;
            for(int i = 0; i < m_; i++) {
                if(edges[i][2] == 0) {
                    edges[i][2] = xor;
                    bad = edges[i][3];
                    break;
                }
            }
            Arrays.sort(edges, Comparator.comparing(arr -> arr[2]));
            for(int i = 0; i < m_; i++) {
                if(edges[i][3] == bad) {
                    bad = i;
                    break;
                }
            }
            p = new int[n];
            sz = new int[n];
            ans = Math.min(ans, doit(edges, n, m_));
            for(int i = 0; i < rem-1; i++) {
                int temp = edges[i][0]; edges[i][0] = edges[bad][0]; edges[bad][0] = temp;
                temp = edges[i][1]; edges[i][1] = edges[bad][1]; edges[bad][1] = temp;
                ans = Math.min(ans, doit(edges, n, m_));
            }            
        } else {
            ArrayDeque<int[]>[] adj = new ArrayDeque[n];
            for(int i = 0; i < n; i++) adj[i] = new ArrayDeque<>();
            for(int i = 0; i < m; i++) {
                int u = sc.nextInt()-1;
                int v = sc.nextInt()-1;
                int w = sc.nextInt();
                adj[u].add(new int[] {v, w});
                adj[v].add(new int[] {u, w});
            }
            boolean[] done = new boolean[n];
            int[] comp = new int[n];
            int comp_sz = 0;
            HashSet<Integer> set = new HashSet<>();
            for(int u = 0; u < n; u++) set.add(u);
            for(int i = 0; i < n; i++) {
                if(!done[i]) {
                    ArrayDeque<Integer> queue = new ArrayDeque<>();
                    queue.add(i);
                    done[i] = true;
                    set.remove(i);
                    while(!queue.isEmpty()) {
                        int u = queue.remove();
                        comp[u] = comp_sz;
                        HashSet<Integer> rem_set = new HashSet<>();
                        for(int[] vv : adj[u]) {
                            int v = vv[0];
                            if(!done[v]) {
                                set.remove(v);
                                rem_set.add(v);
                            }
                        }
                        for(int x : set) {
                            done[x] = true;
                            queue.add(x);
                        }
                        set = rem_set;
                    }
                    comp_sz++;
                }
            }
            p = new int[comp_sz];
            sz = new int[comp_sz];
            for(int u = 0; u < comp_sz; u++) make_set(u);
            long cost = 0L;
            int[][] edges = new int[m][];
            int pos = 0;
            for(int u = 0; u < n; u++) {
                for(int[] vv : adj[u]) {
                    int v = vv[0];
                    if(v < u) continue;
                    int w = vv[1];
                    edges[pos++] = new int[] {comp[u], comp[v], w};
                }
            }
            Arrays.sort(edges, Comparator.comparing(arr -> arr[2]));
            for(int i = 0; i < m; i++) {
                if(union(edges[i][0], edges[i][1])) cost += edges[i][2];
            }
            ans = Math.min(ans, cost);
        }
        println(ans);
    }
    
    long doit(int[][] edges, int n, int m_) {
        for(int u = 0; u < n; u++) make_set(u);
        long cost = 0L;
        int cnt = 0;
        for(int j = 0; j < m_ && cnt < n-1; j++) {
            if(union(edges[j][0], edges[j][1])) {
                cost += edges[j][2];
                cnt++;
            }
        }
        return cost;
    }
//  Solution() throws FileNotFoundException { super(new File("test_output.txt")); }
//  InputReader sc = new InputReader(new FileInputStream("ts1_input.txt"));
  Solution() { super(System.out); }
  InputReader sc = new InputReader(System.in);
  static class InputReader {
      InputReader(InputStream in) { this.in = in; } InputStream in;
      
      private byte[] buf = new byte[16384];
      private int    curChar;
      private int    numChars;
      
 
      public int read() {
          if (numChars == -1)
              throw new InputMismatchException();
          if (curChar >= numChars) {
              curChar = 0;
              try {
                  numChars = in.read(buf);
              } catch (IOException e) {
                  throw new InputMismatchException();
              }
              if (numChars <= 0)
                  return -1;
          }
          return buf[curChar++];
      }
 
      public String nextLine() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          StringBuilder res = new StringBuilder();
          do {
              res.appendCodePoint(c);
              c = read();
          } while (!isEndOfLine(c));
          return res.toString();
      }
 
      public String nextString() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          StringBuilder res = new StringBuilder();
          do {
              res.appendCodePoint(c);
              c = read();
          } while (!isSpaceChar(c));
          return res.toString();
      }
 
      public long nextLong() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          int sgn = 1;
          if (c == '-') {
              sgn = -1;
              c = read();
          }
          long res = 0;
          do {
              if (c < '0' || c > '9')
                  throw new InputMismatchException();
              res *= 10;
              res += c - '0';
              c = read();
          } while (!isSpaceChar(c));
          return res * sgn;
      }
 
      public int nextInt() {
          int c = read();
          while (isSpaceChar(c))
              c = read();
          int sgn = 1;
          if (c == '-') {
              sgn = -1;
              c = read();
          }
          int res = 0;
          do {
              if (c < '0' || c > '9')
                  throw new InputMismatchException();
              res *= 10;
              res += c - '0';
              c = read();
          } while (!isSpaceChar(c));
          return res * sgn;
      }
 
      private boolean isSpaceChar(int c) {
          return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
      }
 
      private boolean isEndOfLine(int c) {
          return c == '\n' || c == '\r' || c == -1;
      }
  }
 
    public static void main(String[] $) {
        new Thread(null, new Runnable() {
            public void run() {
                long start = System.nanoTime();
                try {Solution solution = new Solution(); solution.solve(); solution.flush();} 
                catch (Exception e) {e.printStackTrace(); System.exit(1);}
                System.err.println((System.nanoTime()-start)/1E9);
            }
        }, "1", 1 << 27).start();
 
    }
}