#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=2e5+5;
int n,m;
struct EDGE
{
int left,right;
int value;
};
EDGE edge[maxn];
int father[maxn];
int succeed=0,ans=0;
int read()
{
int f=1,x=0;
char c;
c=getchar();
if(c=='-')f=-1,c=getchar();
while('0'<=c&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
bool cmp(EDGE x,EDGE y)
{
return x.value<y.value;
}
int find(int it)
{
if(it==father[it])return it;
else return father[it]=find(father[it]);
}
int main()
{
n=read(),m=read();
for(int i = 1;i<=n;i++)father[i]=i;
for(int i = 1;i<=m;i++)
{
edge[i].left=read();
edge[i].right=read();
edge[i].value=read();
}
sort(edge+1,edge+1+m,cmp);
for(int i = 1;i<=m;i++)
{
int findl=find(edge[i].left),findr=find(edge[i].right);
if(findl!=findr)
{
father[findr]=findl;
succeed++;ans+=edge[i].value;
}
if(succeed>=n-1)
{
cout<<ans<<endl;
return 0;
}
}
printf("orz\n");
return 0;
}