#include <iostream>
#include <vector>
#include <queue>

using namespace std;

bool vis[100001];
vector < pair < int, int > > v[100011];
priority_queue < pair < pair < int, int >, int > > q;

int main()
{
	long long n, m, w = 0;
	cin >> n >> m;
	for( int i = 1; i <= m; i++)
	{
		int u, o, p;
		cin >> u >> o >> p;
		v[u].push_back( make_pair( p, o ));
		v[o].push_back( make_pair( p, u ));
	}
	q.push(make_pair(make_pair(0, 1), 0));
	while(q.size())
	{
		int t = q.top().first.second;
		int r = q.top().first.first * -1;
		int root = q.top().second;
		q.pop();
		if( vis[t] == true ) continue;
		vis[t] = true;
		w = w + r;
		//cout << root << " " << t << endl;
		for(int i = 0; i < v[t].size(); i++)
			if( vis[v[t][i].second] == false ) q.push(make_pair(make_pair( -1*v[t][i].first, v[t][i].second ), t));
		while( !q.empty() && vis[q.top().first.second] == true ) q.pop();
	}
	cout << w;
	return 0;
}