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

#include<bits/stdc++.h>
using namespace std;
 
#define ios ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ;
#define endl "\n"
#define f1(i,n) for (int i=0;i<n;i++)
#define f2(i,n) for (int i=1;i<=n;i++)
#define int long long
 
typedef vector<int> vi ;
typedef queue<int> qi ;
typedef vector<vector<int>> vvi ;
//typedef vector<vector<ll>> vvll ;
typedef vector<vector<pair<int,int>>> vvp ;
typedef vector<bool> vb ;
typedef vector<double> vd ;
typedef vector<vector<char>> vvc ;
typedef vector<char> vc ;
typedef pair<int,int> pii ;
typedef vector<pair<int,int>> vpi ;
//typedef vector<pair<ll,ll>> vpll ;
//typedef pair<ll,ll> pll ;
//typedef vector<ll> vll ;
typedef stack<int> si ;
typedef map<int,int> mii ;
typedef set<int> seti ;
typedef double db ;
 
const int lim = 1000000+100 ;
int mod1 = 998244353 ;
int mod2 = 1000000007 ;
int INF = 2000000000 ;
int M=1000000000000000000+10000000 ;
const int MAXN=40000+100 ;
 
// **********************************************************
 
int min(int x,int y) {
    if(x<y) return x;
    else return y;
}
 
int max(int x,int y) {
    if(x>y) return x;
    else return y;
}
 
int gcd (int a, int b) {
    if (b == 0)
        return a;
    else
        return gcd (b, a % b);
}
 
int binpow(int a, int b) {
    if (b == 0)
        return 1;
    int res = binpow(a, b / 2);
    if (b % 2)
        return (((res * res)) * a) ;
    else
        return (res * res) ;
}
 
// ***********************************************************
vvi path(lim) ;
vi parent(lim) ;
vi size(lim) ;
seti vertices ;
int rem ;
vi parent1(lim) ;
vi size1(lim) ;

int find_set(int v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}

void make_set(int v) {
    parent[v] = v;
    size[v] = 1;
}

void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (size[a] < size[b])
            swap(a, b);
        parent[b] = a;
        size[a] += size[b];
    }
}

int find_set1(int v) {
    if (v == parent1[v])
        return v;
    return parent1[v] = find_set1(parent1[v]);
}

void make_set1(int v) {
    parent1[v] = v;
    size1[v] = 1;
}

void union_sets1(int a, int b) {
    a = find_set1(a);
    b = find_set1(b);
    if (a != b) {
        if (size1[a] < size1[b])
            swap(a, b);
        parent1[b] = a;
        size1[a] += size1[b];
    }
}

void dfs(int u) {
    vertices.erase(u) ;
    sort(path[u].begin(), path[u].end());
    for (int v = 0;;) {
        auto it = vertices.lower_bound(v);
        if (it == vertices.end()) {
            return;
        }
        v = *it;
        if (!binary_search(path[u].begin(), path[u].end(), v)) {
            union_sets(u, v);
            dfs(v);
            rem--;
        }
        v++;
    }
}

signed main() {
    int n,m ;
    cin >> n >> m ;
    rem=n ;
    int u[m+1],v[m+1],w[m+1] ;
    int x=0 ;
    set<pair<int,pii>> edges ;
    f2(i,n) vertices.insert(i) ;
    f2(i,m) {
        cin >> u[i] >> v[i] >> w[i] ;
        path[u[i]].push_back(v[i]) ;
        path[v[i]].push_back(u[i]) ;
        x^=w[i] ;
        edges.insert({w[i],{u[i],v[i]}}) ;
    }
//    cout << x ;
    f2(i,n) make_set(i) ;
    f2(i,n) make_set1(i) ;
    f2(i,n) {
        if(vertices.count(i)) dfs(i) ;
    }
    int count=0 ;
    mii map1 ;
    f2(i,n) {
        int y=find_set(i) ;
        if(!map1.count(y)) {
            map1[y]=1 ;
            count++ ;
        }
    }
    int c=(n*(n-1))/2-m ;
    bool p=0 ;
    if(c==n-count) p=1 ;
    int res=0 ;
    for(auto it=edges.begin();it!=edges.end();++it) {
        int u=it->second.first ;
        int v=it->second.second ;
        int u1=find_set1(u) ;
        int v1=find_set1(v) ;
        u=find_set(u) ;
        v=find_set(v) ;
        if(u!=v) {
            union_sets(u, v) ;
            union_sets1(u1, v1) ;
            count-- ;
            res+=it->first ;
        }
        else if(u1!=v1 && it->first<x) {
//            cout << x ;
            x=it->first ;
        }
    }
    if(p) cout << res+x ;
    else cout << res ;
}
