#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll fa[5010];
ll get(ll x) {
	return x==fa[x]?x:fa[x]=get(fa[x]);
}
void mrg(ll a,ll b) {
fa[get(a)]=get(b);
}
struct pl{
	ll x,y,z;
	bool operator<(const pl &a){
	return z<a.z;
	}
}mp[200001];
int main() {
ll i,j,n,m,t=0,k=0;
cin>>n>>m;
for(i=1;i<=m;i++)
{
	cin>>mp[i].x>>mp[i].y>>mp[i].z;
}
sort(mp+1,mp+1+m);
for(i=1;i<=n;i++)fa[i]=i;
for(i=1;i<=m&&t<n;i++){
	if(get(mp[i].x)!=get(mp[i].y))mrg(mp[i].x,mp[i].y),t++,k+=mp[i].z;	
}
if(t==n-1)
cout<<k;
else cout<<"orz";
}