import sys sys.setrecursionlimit(10 ** 8) N,M = map(int,input().split()) edge = [] C = 15 mask = 1 << C for _ in range(M): a,b,w = map(int,input().split()) edge.append((w,(a << C) + b)) edge.sort() ans = 0 parent = [-1] * N def find(i): if parent[i] < 0:return i parent[i] = find(parent[i]) return parent[i] def unite(i,j): I = find(i) J = find(j) if I == J:return False if parent[I] < parent[J]: I,J = J,I parent[J] += parent[I] parent[I] = J #parent[i] = J #parent[j] = J return True def isSame(i,j): return find(i) == find(j) for w,k in edge: a = k // mask b = k % mask #print(w,a,b) a -= 1 b -= 1 A = find(a) B = find(b) if A == B:continue na = -parent[A] nb = -parent[B] ans += w * na * nb unite(a,b) #print(na,nb,w) print(ans)