const
max=100000;
type
we=array[1..max]of longint;
var
v,u,w,f:we;
i,a,n,m,b,s,p:longint;
procedure swap(x,y:longint);
var
ls:longint;
begin
ls:=w[x];
w[x]:=w[y];
w[y]:=ls;
ls:=v[x];
v[x]:=v[y];
v[y]:=ls;
ls:=u[x];
u[x]:=u[y];
u[y]:=ls;
end;
procedure qsort(l,r:longint);
var
xl,xr:longint;
x:longint;
begin
x:=w[(l+r)div 2];
xl:=l;
xr:=r;
if xl<xr then
begin
while w[xl]<x do inc(xl);
while w[xr]>x do dec(xr);
if xl<=xr then
begin
swap(xl,xr);
inc(xl);dec(xr);
end;
end;
if xr>l then qsort(l,xr);
if xl<r then qsort(xl,r);
end;
procedure ins;
begin
readln(m,n);
for i:=1 to n do
begin
readln(a,b,s);
u[i]:=a;
v[i]:=b;
w[i]:=s;
end;
qsort(1,n);
end;
procedure chushi;
begin
for i:=1 to n do
begin
f[u[i]]:=u[i];
f[v[i]]:=v[i];
end;
end;
function getfather(var x:longint):longint;
var
y:longint;
begin
y:=x;
while f[x]<>x do x:=f[x];
f[y]:=x;
exit(x);
end;
procedure hb(x,y:longint);
var
fx,fy:longint;
begin
fx:=getfather(x);
fy:=getfather(y);
f[fx]:=fy;
end;
procedure main;
var
xx:longint;
begin
p:=0;
xx:=0;
i:=0;
while xx<m-1do begin
i:=i+1;
if (getfather(u[i]))<>(getfather(v[i])) then
begin
p:=p+w[i];
xx:=xx+1;
hb(u[i],v[i]);
end;
end;
end;
procedure print;
begin
writeln(p);
end;
begin
ins;
chushi;
main;
print;
end.
