I,R=input,range def J(): return map(int,I().split()) N,M=J() p=[i for i in R(N)] c=[1]*N def r(i): m=p[i] while i!=m: p[i]=i=p[m] m=p[i] return i E=[0]*M for i in R(M): a,b,w=J() E[i]=w,a-1,b-1 E.sort() s=0 for i in R(M): w,a,b=E[i] a,b=r(a),r(b) if a!=b: s,c[b],p[a]=s+c[a]*c[b]*w,c[b]+c[a],b print(s)