#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct edge
{
ll x,y,w;
bool operator<(const edge &p) const
{
return w<p.w;
}
} e[100005];
vector<edge>v;
ll pr[10005];
ll find(ll src)
{
if(pr[src]==src)
return src;
return pr[src]=find(pr[src]);
}
ll mst(ll n)
{
sort(v.begin(),v.end());
ll s=0,len=v.size(),cnt=0,i;
for(i=1; i<=n; i++)
pr[i]=i;
for(i=0; i<len; i++)
{
ll u=find(v[i].x);
ll v1=find(v[i].y);
if(u!=v1)
{
pr[u]=v1;
cnt++;
s+=v[i].w;
if(cnt==n-1)
break;
}
}
return s;
}
int main()
{
ll i,j,n,m,t,a,b,c;
scanf("%lld%lld",&n,&m);
for(i=0; i<m; i++)
{
scanf("%lld%lld%lld",&a,&b,&c);
edge g;
g.x=a;
g.y=b;
g.w=c;
v.push_back(g);
}
printf("%lld\n",mst(n));
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgbGwgbG9uZyBsb25nCgpzdHJ1Y3QgZWRnZQp7CiAgICBsbCB4LHksdzsKICAgIGJvb2wgb3BlcmF0b3I8KGNvbnN0IGVkZ2UgJnApIGNvbnN0CiAgICB7CiAgICAgICAgcmV0dXJuIHc8cC53OwogICAgfQp9IGVbMTAwMDA1XTsKCnZlY3RvcjxlZGdlPnY7CgpsbCBwclsxMDAwNV07CgpsbCBmaW5kKGxsIHNyYykKewogICAgaWYocHJbc3JjXT09c3JjKQogICAgICAgIHJldHVybiBzcmM7CiAgICByZXR1cm4gcHJbc3JjXT1maW5kKHByW3NyY10pOwp9CgpsbCBtc3QobGwgbikKewogICAgc29ydCh2LmJlZ2luKCksdi5lbmQoKSk7CiAgICBsbCBzPTAsbGVuPXYuc2l6ZSgpLGNudD0wLGk7CgogICAgZm9yKGk9MTsgaTw9bjsgaSsrKQogICAgICAgIHByW2ldPWk7CiAgICBmb3IoaT0wOyBpPGxlbjsgaSsrKQogICAgewogICAgICAgIGxsIHU9ZmluZCh2W2ldLngpOwogICAgICAgIGxsIHYxPWZpbmQodltpXS55KTsKICAgICAgICBpZih1IT12MSkKICAgICAgICB7CiAgICAgICAgICAgIHByW3VdPXYxOwogICAgICAgICAgICBjbnQrKzsKICAgICAgICAgICAgcys9dltpXS53OwogICAgICAgICAgICBpZihjbnQ9PW4tMSkKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBzOwp9CgppbnQgbWFpbigpCnsKICAgIGxsIGksaixuLG0sdCxhLGIsYzsKICAgIHNjYW5mKCIlbGxkJWxsZCIsJm4sJm0pOwoKICAgIGZvcihpPTA7IGk8bTsgaSsrKQogICAgewogICAgICAgIHNjYW5mKCIlbGxkJWxsZCVsbGQiLCZhLCZiLCZjKTsKICAgICAgICBlZGdlIGc7CiAgICAgICAgZy54PWE7CiAgICAgICAgZy55PWI7CiAgICAgICAgZy53PWM7CiAgICAgICAgdi5wdXNoX2JhY2soZyk7CiAgICB9CiAgICBwcmludGYoIiVsbGRcbiIsbXN0KG4pKTsKfQo=