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 sys
from sys import stdin
from collections import deque

def uf_find(n,p):

    ufl = []

    while p[n] != n:
        ufl.append(n)
        n = p[n]

    for i in ufl:
        p[i] = n

    return n


def uf_union(a,b,p,rank):

    ap = uf_find(a,p)
    bp = uf_find(b,p)

    if ap == bp:
        return True
    else:

        if rank[ap] > rank[bp]:
            p[bp] = ap
        elif rank[ap] < rank[bp]:
            p[ap] = bp
        else:
            p[bp] = ap
            rank[ap] += 1

        return False

n,m = map(int,stdin.readline().split())

if n <= 1000:

    lis = [[0] * n for i in range(n)]
    wuv = []
    XOR = 0
    
    for i in range(m):
        u,v,w = map(int,stdin.readline().split())
        u -= 1
        v -= 1

        wuv.append( (w,u,v) )
        lis[u][v] = lis[v][u] = w
        XOR ^= w

    wuv.sort()

    p = [i for i in range(n)]
    rank = [0] * n
    edge = []
    flag = False

    for i in range(n):
        for j in range(i):
            if lis[i][j] == 0:
                if uf_union(i,j,p,rank):
                    flag = True
                else:
                    edge.append( (i,j,0) )

    nans = 0
    for w,u,v in wuv:
        if uf_union(u,v,p,rank):
            pass
        else:
            nans += w
            edge.append((u,v,w))
                    
    if flag:
        print (nans)
        sys.exit()

    p = [i for i in range(n)]
    rank = [0] * n
    for i,j,w in edge:
        if w > 0:
            uf_union(i,j,p,rank)

    nplus = XOR
    for w,u,v in wuv:
        if uf_find(u,p) == uf_find(v,p):
            nplus = min(nplus,w)
            break
        elif w > nplus:
            break

    print (nans + nplus)

else:

    lis = [ [] for i in range(n) ]
    wuv = []
    for i in range(m):
        u,v,w = map(int,stdin.readline().split())
        u -= 1
        v -= 1
        wuv.append((w,u,v))

        lis[u].append( (v,w) )
        lis[v].append( (u,w) )

    able = [True] * n
    p = [i for i in range(n)]
    rank = [0] * n

    for st in range(n):

        if not able[st]:
            continue

        block = [0] * n
        cnt = 0
        able[st] = False

        q = deque([st])

        while q:
            
            while q:    
                nv = q.popleft()
                cnt += 1
                for u,w in lis[nv]:
                    if able[u] and block[u] == cnt - 1:
                        block[u] = cnt
            

            for i in range(n):
                if able[i] and block[i] != cnt:
                    uf_union(i,st,p,rank)
                    able[i] = False
                    q.append(i)

    wuv.sort()
    ans = 0
    for w,u,v in wuv:
        if not uf_union(u,v,p,rank):
            ans += w

    print (ans)
                    
    