#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int mx = 1e5+10;
int fa[mx];
long long sum;
int n,ce,m;
struct aa{
int x,y,qu;
}p[mx*5];
void init(){
// memset(vis, 0, sizeof(vis));
for(int i = 1; i < mx; i++ )
fa[i] = i;
sum=ce=0;
}
bool cmp(aa &a,aa &b){
return a.qu<b.qu;
}
int find(int x){
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
//int a,b,v;
while(m--){
scanf("%d%d%d",&p[ce].x,&p[ce].y,&p[ce].qu);
ce++;
}
sort(p,p+ce,cmp);
int x,y,qu,a,b;
for(int i = 0; i < ce; i++){
x = p[i].x,y = p[i].y, qu = p[i].qu;
a=find(x),b=find(y);
if(a == b) continue;
else{
fa[a] = b;
sum += qu;
}
}
cout<<sum<<endl;
}
return 0;
}