#include<bits/stdc++.h>
using namespace std;
#define re register
#define MAXN 5010
struct side
{
int u,v,w;
}s[200010];
int fa[MAXN],n,m,ans,cnt,cnt1;
inline int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
bool cmp(side a,side b)
{return a.w<b.w;}
int main()
{
scanf("%d%d",&n,&m);
for(re int i=1;i<=n;++i) fa[i]=i;
for(re int i=1;i<=m;++i)
{
scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].w);
}
sort(s+1,s+1+m,cmp);
for(re int i=1;i<=m;++i)
{
if(find(s[i].u)!=find(s[i].v))
{
fa[find(s[i].v)]=find(s[i].u);
ans+=s[i].w;
}
}
for(re int i=2;i<=n;++i)
{
if(find(i)!=find(i-1))
{
printf("orz");
return 0;
}
}
printf("%d",ans);
return 0;
}