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
input = sys.stdin.readline

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    n, m = map(int, input().split())
    if n == 2:
        if m == 1:
            print(list(map(int, input().split()))[2])
        else:
            print(0)
        return
    edges = []
    tf = set()
    xx = 0
    se = set()
    for _ in range(m):
        u, v, x = map(int, input().split())
        u -= 1
        v -= 1
        xx ^= x
        if u > v: u,v = v,u
        edges.append((x, u, v))
        tf.add("{}-{}".format(u,v))
        se.add(u)
        se.add(v)
    edges.sort()
    
        
    l = n * (n - 1) // 2 - m
    ans = 0
    se = set(range(1, n))
    UF = UnionFind(n)
    for p in range(n):
        rem = set()
        for np in se:
            if p > np:
                rem.add(np)
                continue
            s = "{}-{}".format(p, np)
            if s in tf:
                continue
            if not UF.same(p, np):
                rem.add(np)
                UF.union(p, np)
                l -= 1
        for s in rem:
            se.remove(s)
        
    for x, u, v in edges:
        if not UF.same(u, v):
            ans += x
            UF.union(u, v)
    
    if l != 0:
        print(ans)
        return
    
    else:
        ans1 = ans + xx
    
    def solve(use):
        l = n * (n - 1) // 2 - m
        ans = 0
        se = set(range(1, n))
        UF = UnionFind(n)
        for x, u, v in edges:
            if not UF.same(u, v):
                ans += x
                UF.union(u, v)
                use -= 1
                if use == 0:
                    break
        for p in range(n):
            rem = set()
            for np in se:
                if p > np:
                    rem.add(np)
                    continue
                s = "{}-{}".format(p, np)
                if s in tf:
                    continue
                if not UF.same(p, np):
                    rem.add(np)
                    UF.union(p, np)
                    l -= 1
        for s in rem:
            se.remove(s)
        for x, u, v in edges:
            if not UF.same(u, v):
                ans += x
                UF.union(u, v)
        if l > 0:
            return ans
        else:
            return -1
            
    l = 0
    r = len(se) - 1
    while r - l > 1:
        mid = (r + l) // 2
        z = solve(mid)
        if z == -1:
            l = mid
        else:
            r = mid
    print(min(ans1, solve(r)))
    
    

    
for _ in range(1):
    main()