import sys input = sys.stdin.readline from heapq import heappop,heappush N,M=map(int,input().split()) E=[[] for i in range(N)] for i in range(M): a,b,c=map(int,input().split()) a-=1 b-=1 E[a].append((b,c)) E[b].append((a,c)) # UnionFind Group = [i for i in range(N+1)] # グループ分け Nodes = [1]*(N+1) # 各グループのノードの数 def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if Nodes[find(x)] < Nodes[find(y)]: Nodes[find(y)] += Nodes[find(x)] Nodes[find(x)] = 0 Group[find(x)] = find(y) else: Nodes[find(x)] += Nodes[find(y)] Nodes[find(y)] = 0 Group[find(y)] = find(x) ANS=0 H=[] USE=[0]*N USE[0]=1 for to,w in E[0]: heappush(H,(-w,to)) while H: #print(H) while H and USE[H[0][1]]==1: heappop(H) #print(H) if H: w,to=heappop(H) ANS-=w USE[to]=1 for toto,w in E[to]: if USE[toto]==0: heappush(H,(-w,toto)) else: break print(ANS*2)