n,m=map(int,input().split()) e=[] for _ in range(m): a,b,c=map(int,input().split()) a-=1 b-=1 e+=[(c,a,b)] e.sort() r=list(range(n)) def union(x,y): rx=root(x) ry=root(y) if rx==ry: return if rx>ry: rx,ry=ry,rx r[ry]=rx return 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] ans=0 for c,a,b in e[::-1]: if root(a)!=root(b): union(a,b) ans+=c print(ans*2)