Link to this code: https://cses.fi/paste/f81795c36e800899ad21dd/
// https://cses.fi/problemset/task/1675, Kruskal
#include "bits/stdc++.h"
using namespace std;
using LL = long long;

struct Dsu {
	int n;
	vector<int> p, s;
	Dsu(int n) : n(n), p(n), s(n, 1) {
		iota(begin(p), end(p), 0);
	}
	int find(int v) {
		return p[v] == v ? v : p[v] = find(p[v]);
	}
	bool merge(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b)
			return false;
		if (s[a] < s[b])
			swap(a, b);
		p[b] = a;
		s[b] += s[a];
		return true;
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n; cin >> n;
	int m; cin >> m;
	vector<array<int, 3>> e(m);
	for (auto& [a, b, w] : e) {
		cin >> a >> b >> w;
		--a, --b;
	}
	sort(begin(e), end(e), [](array<int, 3> x, array<int, 3> y){
		return x[2] < y[2];
	});
	int cc = n;
	LL ans = 0;
	Dsu dsu(n);
	for (auto [a, b, w] : e) {
		if (dsu.merge(a, b)) {
			--cc;
			ans += w;
		}
	}
	if (cc == 1) {
		cout << ans << "\n";
	} else {
		cout << "IMPOSSIBLE\n";
	}
}