#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=300000;
int head[maxn],cnt,n,m,sm,f[maxn],cost=0;
struct e{
    int u,v,w;
}edge[maxn*2];
bool cmp(e i,e j)
{
    return i.w<j.w;
}
int find(int x)//并查集
{
    if(x==f[x]) return f[x];
    else return f[x]=find(f[x]);
}
signed main()
{
    scanf("%lld %lld",&n,&m);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        scanf("%lld %lld %lld",&u,&v,&w);
        edge[i].u=u,edge[i].v=v,edge[i].w=w;
    }
    sort(edge+1,edge+1+m,cmp);
    int i=0;
    for(i=1;i<=m;i++)
    {
        int r=i;
        while(r<=m&&edge[i].w==edge[r].w) r++;
        for(int h=i;h<r;h++)
        {
            int eu=find(edge[h].u),ev=find(edge[h].v);
            if(eu!=ev) sm+=edge[h].w;
        }
        for(int h=i;h<r;h++)
        {
            int eu=find(edge[h].u),ev=find(edge[h].v);
            if(eu!=ev) f[eu]=ev,sm-=edge[h].w;
        }
        i=r-1;
    }
    cout<<sm;
}