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

#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

class DisjointSetUnion:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n
        self.num_sets = n

    def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a, b):
        a, b = self.find(a), self.find(b)
        if a != b:
            if self.size[a] < self.size[b]:
                a, b = b, a

            self.num_sets -= 1
            self.parent[b] = a
            self.size[a] += self.size[b]

    def set_size(self, a):
        return self.size[self.find(a)]

    def __len__(self):
        return self.num_sets


class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a, b):
        self.parent[self.find(b)] = self.find(a)

def main():
    n,m = map(int,input().split())
    adjList = []
    for _ in range(n):
        adjList.append([])
    edges = []
    lastEdge = 0
    for _ in range(m):
        p,q,w = map(int,input().split())
        edges.append((w,p - 1,q - 1))
        adjList[p - 1].append(q - 1)
        adjList[q - 1].append(p - 1)
        lastEdge ^= w
    for i in range(n):
        adjList[i].sort()
    
    edges.sort()

    DSU = UnionFind(n)

    mergeCnt = 0

    for i in range(n):
        curAdjIndex = 0
        for j in range(n):
            if curAdjIndex < len(adjList[i]) and adjList[i][curAdjIndex] == j:
                curAdjIndex += 1
                continue
            if DSU.find(i) == DSU.find(j):
                continue
            mergeCnt += 1
            DSU.union(i, j)
            if mergeCnt == n - 1:
                break
        if mergeCnt == n - 1:
            break

    minAns = 0
    flag = False
    if mergeCnt == n * (n - 1) // 2 - m:
        flag = True
        tempColor = []
        for i in range(n):
            if DSU.find(i) != i:
                tempColor.append(DSU.find(i))
        tempColor.sort()
        newTempColor = []
        for i in range(len(tempColor)):
            if i == 0 or tempColor[i] != tempColor[i - 1]:
                newTempColor.append(tempColor[i])

    for elem in edges:
        if DSU.find(elem[1]) != DSU.find(elem[2]):
            minAns += elem[0]
            DSU.union(elem[1], elem[2])
        else:
            if flag:
                c = DSU.find(elem[1])
                l = 0
                r = len(newTempColor) - 1
                while l < r:
                    m = (l + r) // 2
                    if c == newTempColor[m]:
                        l = m
                        break
                    elif c < newTempColor[m]:
                        r = m - 1
                    else:
                        l = m + 1
                if newTempColor[l] == c:
                    minAns += min(elem[0], lastEdge)
                    flag = False
    
    print(minAns)



# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()