#include<bits/stdc++.h>
using namespace std;
struct edge{
int u,v,w;
};
const int yhy=200005,dumpling=5005;
int n,m,f [dumpling],ans;
edge a[yhy];
int find(int x){
if(x==f[x])
return x;
return f[x]=find(f[x]);
}
bool cmp(edge x,edge y){
return x.w<y.w;
}
void cxk(){
for(int i=1;i<=n;i++)
f[n]=n;
}
void kkk(){
sort(a+1,a+m+1,cmp);
int tot=0;
for(int i=1;i<=m;i++){
int fu=find(a[i].u),fv=find(a[i].v);
if(fu==fv)
continue;
f[fu]=fv;
ans+=a[i].w;
if(++tot==n+1)
return;
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>a[i].u>>a[i].v>>a[i].w;
kkk();
cout<<ans;
return 0;
}