#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long int lli;
typedef pair<int,int> mp;
typedef pair<int,mp> mmp;
#define fir first
#define sec second


struct unifo{
	int un;
	int uni[100005];
	void init(int n){
		rep(i,n)uni[i]=i;
		un=n;
	}
	int find(int p){
		if(uni[p]==p)return p;
		else return (uni[p]=find(uni[p]));
	}
	void mer(int p,int q){
		uni[find(p)]=find(q);
	}
	bool same(int p,int q){
		return find(p)==find(q);
	}
};

	
int kruskal(int n,vector<mmp>& es){
	unifo u;
	u.init(n);
	sort(es.begin(),es.end());
	int res=0;
	rep(i,es.size()){
		mmp pa=es[i];
		int a=pa.sec.fir,
			b=pa.sec.sec,
			c=pa.fir;
		if(!u.same(a,b)){
			res+=c;
			u.mer(a,b);
		}
	}
	return res;
}

	
int n,m;

//vector<mp> vs[105];


vector<mmp> es;


int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	
	rep(i,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		//vs[a].push_back(mp(b,c));
		//vs[b].push_back(mp(a,c));
		es.push_back(mmp(c,mp(a,b)));
	}
	printf("%d\n",kruskal(n,es));
	return 0;
}
//*********************************************************************************************************************************
//*																																**
//*		SSSSSSSSSSSS											MMMM			MMMM	HHHH		HHHH	DDDDDDDDDDDD		**
//*		SSSSSSSSSSSS	CSAA Numbered							MMMMM		   MMMMM	HHHH		HHHH	DDDDDDDDDDDDD		**
//*		SSSS				A PROGRAM BY:						MMMMMM		  MMMMMM	HHHH		HHHH	DDDD	 DDDDD		**
//*		SSSS													MMMMMMM		 MMMMMMM	HHHH		HHHH	DDDD	   DDDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAA	MMMMMMMMMMMMMMMMMMMM	MMMMMMMM	MMMMMMMM	HHHHHHHHHHHHHHHH	DDDD	    DDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAA	MMMMMMMMMMMMMMMMMMMM	MMMM MMMM  MMMM MMMM	HHHHHHHHHHHHHHHH	DDDD	    DDD		**
//*				SSSS	AAAA	AAAA	MMMM	MMMM	MMMM	MMMM  MMMMMMMM	MMMM	HHHH		HHHH	DDDD	   DDDD		**
//*				SSSS	AAAA	AAAA	MMMM	MMMM	MMMM	MMMM   MMMMMM	MMMM	HHHH		HHHH	DDDD	  DDDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAAA	MMMM	MMMM	MMMM	MMMM    MMMM	MMMM	HHHH		HHHH	DDDDDDDDDDDDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAAAA	MMMM	MMMM	MMMM	MMMM			MMMM	HHHH		HHHH	DDDDDDDDDDDD		**
//*																																**
//********************************************************************************************************************************
//||											||
//||	CSAA AA###B ddmmYYYY					||
//||	?? SMP(SAM-MHD-PGM) @ AEH_COT96			||
//||	Program Name:							||
//||			AA###B_<xxxxNAMExxxx>			||
//||											||
/////******************************************\\

#include <iostream>
#include <vector>

using namespace std;
const int maxn = 1e4+3;
const int oo   = 3e7+3;

typedef pair <int, int> pii;
vector <pii> G[maxn];
vector <pii> ans;
bool used[maxn]; // Already in MST
long long pim [maxn]; // Partner in MST
long long mdti[maxn]; // Min_dis to MST :: Minimal Distance to Include
long long n, m;
long long ansvalue;

bool MST(){
	
	fill_n(mdti, n, oo); 
	fill_n(pim, n, -1);  
	fill_n(used, n, false); 
	
	mdti[0] = 0;
	
	for (int i = 0; i < n; i++) {
		
		int v = -1;
		
		for (int j=0; j<n; j++)
			if ( !used[j] && (v == -1 || mdti[j] < mdti[v]))
				v = j;
 
//		if (mdti[v] == oo) 
//			return false;
 
		used[v] = true;
		
		if (pim[v] != -1){
			ans.push_back ( make_pair( pim[v], v ) );
			ansvalue += mdti[v];
		} 
 
		for (int j = 0; j < G[v].size(); j++) 
			if ( G[v][j].second < mdti[ G[v][j].first ] ) {
				mdti[ G[v][j].first ] = G[v][j].second;
				pim [ G[v][j].first ] = v;
			}
	}
	return true;
}

int main() {
	
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int a, b, w;
		cin >> a >> b >> w;
		G [a].push_back( make_pair( b, w) );
		G [b].push_back( make_pair( a, w) ); 
	}
	
//	if (MST())
//		for (pii e : ans)
//			cout << e.first << " " << e.second << endl;
//	else cout << -1;
	if (MST())
		cout << ansvalue << endl;
	
}
/**/

// ?? SMP(SAM-MHD-PGM) @ AEH_COT96
//   SamMHD (Saman Mahdanian)