#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAXM 100000
#define MAXN 10000
#define LL long long int
struct edge
{
int leftnode;
int rightnode;
int data;
edge();
};
edge::edge()
{
leftnode=0;
rightnode=0;
data=0;
}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
edge a[MAXM+1];
int n,m,father[MAXN+1],times=0;
LL ans=0;
int getfather(int x)
{
if(father[x]==x)
return x;
else
return father[x]=getfather(father[x]);
}
void hebin(int x,int y)
{
father[getfather(x)]=father[getfather(y)];
}
int cmp(const void *a,const void *b)
{
return (*(edge *)a).data-(*(edge *)b).data;
}
int main()
{
freopen("kruskal.in","r",stdin);
freopen("kruskal.out","w",stdout);
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>a[i].leftnode>>a[i].rightnode>>a[i].data;
}
qsort(a,m,sizeof(edge),cmp);
int t=0;
while(times!=n-1)
{
if(getfather(a[t].leftnode)!=getfather(a[t].rightnode))
{
ans+=a[t].data;
hebin(a[t].leftnode,a[t].rightnode);
times++;
}
t++;
}
cout<<ans<<endl;
}
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAXM 100000
#define MAXN 10000
#define LL long long int
struct edge
{
int leftnode;
int rightnode;
int data;
edge();
};
edge::edge()
{
leftnode=0;
rightnode=0;
data=0;
}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
edge a[MAXM+1];
int n,m,father[MAXN+1],times=0;
LL ans=0;
int getfather(int x)
{
if(father[x]==x)
return x;
else
return father[x]=getfather(father[x]);
}
void hebin(int x,int y)
{
father[getfather(x)]=father[getfather(y)];
}
int cmp(const void *a,const void *b)
{
return (*(edge *)a).data-(*(edge *)b).data;
}
int main()
{
freopen("kruskal.in","r",stdin);
freopen("kruskal.out","w",stdout);
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>a[i].leftnode>>a[i].rightnode>>a[i].data;
}
qsort(a,m,sizeof(edge),cmp);
int t=0;
while(times!=n-1)
{
if(getfather(a[t].leftnode)!=getfather(a[t].rightnode))
{
ans+=a[t].data;
hebin(a[t].leftnode,a[t].rightnode);
times++;
}
t++;
}
cout<<ans<<endl;
}