type arr=record
u,v,w,nt:longint;
end;
var i,j,m,n,x,y:longint;
ans:int64;
a:array[0..200008] of arr;
fa:array[0..200008] of longint;
function find(x:longint):longint;
begin
if fa[x]=x then exit(x);
fa[x]:=find(fa[x]);
exit(fa[x]);
end;
procedure sort(l,r:longint);
var i,j,x:longint;
temp:arr;
begin
i:=l;j:=r; x:=a[(l+r) div 2].w;
while i<=j do
begin
while a[i].w<x do inc(i);
while x<a[j].w do dec(j);
if i<=j then
begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
inc(i);
dec(j);
end;
end;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
readln(n,m);
for i:=1 to m do readln(a[i].u,a[i].v,a[i].w);
for i:=1 to n do fa[i]:=i;
sort(1,m);
for i:=1 to m do
begin
x:=find(a[i].u);
y:=find(a[i].v);
if x<>y then
begin
ans:=ans+a[i].w;
fa[x]:=y;
end;
end;
writeln(ans);
end.