#include<bits/stdc++.h>
using namespace std;
int n,m,fa[5005],t,sum,v[5005];
struct node {
int from,to,l;
} mp[200005];
bool cmp(node a,node b) {
return a.l<b.l;
}
int find(int x) {
if(fa[x]==x) return x;
fa[x]=find(fa[x]);
return fa[x];
}
int main() {
cin>>n>>m;
for(int i=1; i<=m; ++i)
cin>>mp[i].from>>mp[i].to>>mp[i].l;
sort(mp+1,mp+m+1,cmp);
for(int i=1; i<=n; ++i) fa[i]=i;
for(int i=1; i<=m; ++i) {
if(find(mp[i].from)!=find(mp[i].to)) {
fa[mp[i].to]=mp[i].from;
sum+=mp[i].l;
t++;
}
if(t==n-1) break;
}
if(t==n-1)
cout<<sum;
else
cout<<"orz";
}