n,m=map(int,input().split()) e=[] for i in range(m): a,b,c=map(int,input().split()) a-=1 b-=1 e+=[(c,a,b)] g=0 def root(x): p=x l=[p] while r[p]!=p: p=r[p] l+=[p] for p in l: r[p]=l[-1] return r[x] def union(x,y): global g,c rx=root(x) ry=root(y) if rx==ry: return if rx>ry: rx,ry=ry,rx r[ry]=rx g+=c*v[rx]*v[ry] v[rx]+=v[ry] return r=list(range(n)) v=[1]*n for c,a,b in sorted(e): union(a,b) print(g)