#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#define nmax_def 220000
#define mod 1000000007
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
#define graph_from1 false
#define graph_tree false
#define graph_weight true
#define graph_digraph false

class Graph_func {
public:
	int Vnum;
	int Enum;
	vector<pii> edgelist;
	vl edgeweight;
	vector<vi> adjlist;
	vector<vl> adjlistweight;
	vi adjnum;

	vi adj_to_root;
	vector<int> DP;
	vi vlist_from_root;
	vl xdist;
	
	vb x_visited;
	vi x_prqarr;
	vb x_filled;

	void read_edge();
	void adjini();
	void visited_ini();
	void adjsort();
	vb graph_num_BFS(int BFS_ini, vb filled);
	int graph_num();
	void graph_BFS(ll BFS_ini);
	void graph_DFS(ll DFS_ini);
	void xydist_tree(int x);   // ex. xydist_tree(3)
	void xydist(int x);   // ex. xydist(3, 0)
	ll diameter();
	void ReRooting(ll root);
	Graph_func Kruskal();
	int dim_num(int dim);
	void solve();

	bool xydist_def = false;
};

class UnionFind {
public:
	vi par;
	vi adjnum;
	int N;

	void UnionFind_ini(int Narg);
	int root(int x);
	void unite(int x, int y);
	bool same(int x, int y);
	int adjnum_func(int x);
};

// ----- UnionFind -----

void UnionFind::UnionFind_ini(int Narg) {
	N = Narg;
	adjnum.resize(N);
	par.resize(N);
	rep(i, N) {
		adjnum[i] = 1;
		par[i] = i;
	}
}

int UnionFind::root(int x) {
	if (par[x] == x)
		return x;
	par[x] = root(par[x]);
	return par[x];
}

void UnionFind::unite(int x, int y) {
	int rx = root(x);
	int ry = root(y);
	if (rx == ry)
		return;
	par[rx] = ry;
	adjnum[ry] += adjnum[rx];
	return;
}

bool UnionFind::same(int x, int y) {
	int rx = root(x);
	int ry = root(y);
	return rx == ry;
}

int UnionFind::adjnum_func(int x) {
	int rx = root(x);
	return adjnum[rx];
}

// ----- graph -----

void Graph_func::read_edge() {
	cin >> Vnum;
	if (graph_tree)
		Enum = Vnum - 1;// graph
	else
		cin >> Enum;
	edgelist.resize(Enum);
	edgeweight.resize(Enum, 1);
	adjnum.resize(Vnum);
	xdist.resize(Vnum);
	adjlist.resize(Vnum);
	if(graph_weight)
		adjlistweight.resize(Vnum);
	adj_to_root.resize(Vnum, -1);
	DP.resize(Vnum, 0);
	rep(i, Enum) {
		if (graph_weight)
			cin >> edgelist[i].first >> edgelist[i].second >> edgeweight[i];
		else
			cin >> edgelist[i].first >> edgelist[i].second;
		if (graph_from1) {
			edgelist[i].first--;
			edgelist[i].second--;
		}
	}
	return;
}

void Graph_func::adjini() {
	rep(i, Vnum)
		adjnum[i] = 0;
	rep(i, Enum) {
		int x = edgelist[i].first;
		int y = edgelist[i].second;
		adjlist[x].push_back(y);
		if(!graph_digraph)
			adjlist[y].push_back(x);
		if (graph_weight) {
			ll xyweight = edgeweight[i];
			adjlistweight[x].push_back(xyweight);
			if (!graph_digraph)
				adjlistweight[y].push_back(xyweight);
		}
		adjnum[x]++;
		if (!graph_digraph)
			adjnum[y]++;
	}
	x_visited.resize(Vnum);
	visited_ini();

	return;
}

void Graph_func::visited_ini() {
	rep(i, Vnum)
		x_visited[i] = false;
	x_prqarr.resize(0);
}

void Graph_func::adjsort() {
	rep(i, Vnum)
		sort(adjlist[i].begin(), adjlist[i].end(), less<int>());
	return;
}

vb Graph_func::graph_num_BFS(int BFS_ini, vb filled) { // ex. graph_num_BFS(i, filled);
	x_prqarr.push_back(BFS_ini);
	x_visited[BFS_ini] = true;

	while (!x_prqarr.empty())	{
		int Ntmp = x_prqarr.size();
		vi x_prqarr_next(0);
		rep(n, Ntmp) {
			int x = x_prqarr[n];
			rep(i, adjnum[x]) {
				int x_new = adjlist[x][i];
				if (!x_visited[x_new]) {
					x_prqarr_next.push_back(x_new);
					x_visited[x_new] = true;
					filled[x_new] = true;
				}
			}
		}
		x_prqarr = x_prqarr_next;
	}
	return filled;
}

int Graph_func::graph_num() {
	int anstmp = 0;
	int i = 0;
	vector<bool> filled;
	filled.resize(Vnum);
	rep(j, Vnum)
		filled[j] = false;
	bool end = false;
	while (!end)	{
		if (i >= Vnum)
			break;
		while (1) {
			if (!filled[i])
				break;
			i++;
			if (i >= Vnum) {
				end = true;
				break;
			}
		}
		if (end)
			break;
		filled[i] = true;
		filled = graph_num_BFS(i, filled);
		anstmp++;
	}
	return anstmp;
}

void Graph_func::graph_BFS(ll BFS_ini) {
	visited_ini();
	x_prqarr.push_back(BFS_ini);
	x_visited[BFS_ini] = true;
	vlist_from_root.resize(1, BFS_ini);
	while (!x_prqarr.empty()) {
		vi x_prqarr_next(0);
		int Ntmp = x_prqarr.size();
		rep(n, Ntmp) {
			int x = x_prqarr[n];
			rep(i, adjnum[x]) {
				int x_new = adjlist[x][i];
				if (!x_visited[x_new]) {
					adj_to_root[x_new] = x;
					vlist_from_root.push_back(x_new);
					x_prqarr_next.push_back(x_new);
					x_visited[x_new] = true;
					//cout << x_new << endl;
				}
			}
		}
		x_prqarr = x_prqarr_next;
	}
	return;
}

void Graph_func::graph_DFS(ll DFS_ini) {
	visited_ini();
	x_prqarr.push_back(DFS_ini);
	x_visited[DFS_ini] = true;
	
	while (!x_prqarr.empty()) {
		int Ntmp = x_prqarr.size();
		rep(a, Ntmp) {
			int x = x_prqarr[x_prqarr.size() - 1];
			x_prqarr.pop_back();
			rep(i, adjnum[x]) {
				int x_new = adjlist[x][i];
				if (!x_visited[x_new]) {
					adj_to_root[x_new] = x;
					x_prqarr.push_back(x_new);
					x_visited[x_new] = true;
				}
			}
		}
	}
	return;
}

void Graph_func::xydist_tree(int BFS_ini) {  // ex. xydist_tree(3), BFS
	visited_ini();
	x_prqarr.push_back(BFS_ini);
	x_visited[BFS_ini] = true;
	xdist[BFS_ini] = 0;

	while (!x_prqarr.empty()) {
		vi x_prqarr_next(0);
		int Ntmp = x_prqarr.size();
		rep(n, Ntmp) {
			int x = x_prqarr[n];
			rep(i, adjnum[x]) {
				int x_new = adjlist[x][i];
				int dist_new;
				if (graph_weight)
					dist_new = adjlistweight[x][i];
				else
					dist_new = 1;
				if (!x_visited[x_new]) {
					x_prqarr_next.push_back(x_new);
					xdist[x_new] = xdist[x] + dist_new;
					x_visited[x_new] = true;
					//cout << x_new << endl;
				}
			}
		}
		x_prqarr = x_prqarr_next;
	}
	return;
}

void Graph_func::xydist(int x) {  // ex. xydist(3), Dijkstra's algorithm
	priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > xdist_que; //Alive_vertices
	ll max_tmp = pow(10, 18) * 4;
	rep(i, Vnum)
		xdist[i] = max_tmp;
	xdist[x] = 0;
	pair<ll, int> xdist_que_tmp;
	xdist_que_tmp.first = 0;
	xdist_que_tmp.second = x;
	xdist_que.push(xdist_que_tmp);
	while (!xdist_que.empty()) {
		xdist_que_tmp = xdist_que.top();
		xdist_que.pop();
		ll xdist_tmp = xdist_que_tmp.first;
		int x_tmp = xdist_que_tmp.second;
		if (xdist[x_tmp] == xdist_tmp) {
			rep(i, adjnum[x_tmp]) {
				int x_new = adjlist[x_tmp][i];
				ll dist_diff;
				if(graph_weight)
					dist_diff = adjlistweight[x_tmp][i];
				else dist_diff = 1;
				if (xdist[x_new] > xdist[x_tmp] + dist_diff) {
					xdist[x_new] = xdist[x_tmp] + dist_diff;
					xdist_que_tmp.first = xdist[x_new];
					xdist_que_tmp.second = x_new;
					xdist_que.push(xdist_que_tmp);
				}
			}
		}
	}
}

ll Graph_func::diameter() {
	ll anstmp = 0;
	if (graph_tree) {
		ll anstmp_i = 0;
		pii longest;
		xydist_tree(0);
		rep(i, Vnum) {
			if (xdist[i] > anstmp) {
				anstmp = xdist[i];
				anstmp_i = i;
			}
		}
		longest.first = anstmp_i;
		anstmp = 0, anstmp_i = 0;
		xydist_tree(longest.first);
		rep(i, Vnum) {
			if (xdist[i] > anstmp) {
				anstmp = xdist[i];
				anstmp_i = i;
			}
		}
		longest.second = anstmp_i;
	}
	else {
		rep(i, Vnum) {
			xydist(i);
			rep(j, Vnum) {
				anstmp = max(anstmp, xdist[j]);
			}
		}
	}
	return anstmp;
}


void Graph_func::ReRooting(ll root) {
	rep(i, Vnum) {
		DP[i] = 1;
	}
	graph_BFS(root);
	for (int i = Vnum - 1; i >= 0; i--) { // 根から遠い点から見る
		ll x = vlist_from_root[i];
		rep(j, adjnum[x]) {
			ll x_new = adjlist[x][j];
			if (x_new == adj_to_root[x]) // 根側
				continue;
			DP[x] += DP[x_new];
		}
	}
	return;
}

Graph_func Graph_func::Kruskal() {
	Graph_func tree;
	vector<pair<ll, pii> > edgeweight_sort(Enum);
	rep(i, Enum) {
		edgeweight_sort[i].first = edgeweight[i];
		edgeweight_sort[i].second = edgelist[i];
	}
	sort(edgeweight_sort.begin(), edgeweight_sort.end(), less<pair<ll, pii> >());
	UnionFind tree_uf;
	tree_uf.UnionFind_ini(Vnum);
	tree.Vnum = Vnum;
	tree.Enum = 0;
	rep(i, Enum) {
		ll edgeweighttmp = edgeweight_sort[i].first;
		pii edgetmp = edgeweight_sort[i].second;
		if (!tree_uf.same(edgetmp.first, edgetmp.second)) {
			tree_uf.unite(edgetmp.first, edgetmp.second);
			tree.edgelist.push_back(edgetmp);
			tree.edgeweight.push_back(edgeweighttmp);
			tree.Enum++;
		}
	}
	return tree;
}


int Graph_func::dim_num(int dim) {
	int anstmp = 0;
	rep(i, Vnum) {
		if (adjnum[i] == dim)
			anstmp++;
	}
	return anstmp;
}

void Graph_func::solve() {
	Graph_func tree = Kruskal();
	ll ans = 0;
	rep(i, tree.Enum) {
		ans += tree.edgeweight[i];
		//cout << (tree.edgelist[i].first) << " " << (tree.edgelist[i].second) << " " << tree.edgeweight[i] << endl;
	}
	cout << ans << endl;
	return;
}

int main(void) {
	Graph_func graph;
	Graph_func tree;
	graph.read_edge();
	graph.adjini();
	graph.solve();
	return 0;
}


Measured memory_bytes: 11176