#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int>pi;
typedef pair<ll, ll>pl;
typedef vector<pi>vpi;
typedef vector<pl>vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
const long double PI = acos(-1);
const ll oo = 1e18 + 7;
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define read(v) for (auto& it : v) scanf("%d", &it);
#define readl(v) for (auto& it : v) scanf("%lld", &it);
#define print(v) for (auto it : v) printf("%d ", it); puts("");
#define printl(v) for (auto it : v) printf("%lld ", it); puts("");
int n,m,p[N],cnt=0;
int find(int u){
if(u==p[u])
return u;
return p[u]=find(p[u]);
}
void unite(int u,int v){
u=find(u),v=find(v);
if(u==v)
return;
p[v]=u;
cnt--;
}
void solve() {
scanf("%d %d",&n,&m);
for(int i=0;i<=n;i++)
p[i]=i;
cnt=n;
vector<pair<ll, pl>>edges;
for (int i = 0, a, b, c; i < m; i++) {
scanf("%d %d %d", &a, &b, &c);
edges.push_back({(ll) c,{a,b} });
}
sort(all(edges));
ll ans=0;
for(auto&it:edges){
ll u=it.second.first,v=it.second.second;
ll w=it.first;
u=find(u),v=find(v);
if(u!=v){
ans+=w;
unite(u,v);
}
}
if(cnt>1){
cout<<"IMPOSSIBLE"<<endl;
return;
}
printf("%lld\n",ans);
}
int t = 1;
int main() {
//scanf("%d", &t);
while (t--) solve();
}
I2RlZmluZSBfQ1JUX1NFQ1VSRV9OT19XQVJOSU5HUwojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4JCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIGxvbmcgZG91YmxlIGxkOwp0eXBlZGVmIHZlY3RvcjxpbnQ+IHZp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W3ZdPXU7CiAgICBjbnQtLTsKfQp2b2lkIHNvbHZlKCkgewogICAgc2NhbmYoIiVkICVkIiwmbiwmbSk7CiAgICBmb3IoaW50IGk9MDtpPD1uO2krKykKICAgICAgICBwW2ldPWk7CiAgICBjbnQ9bjsKCXZlY3RvcjxwYWlyPGxsLCBwbD4+ZWRnZXM7Cglmb3IgKGludCBpID0gMCwgYSwgYiwgYzsgaSA8IG07IGkrKykgewoJCXNjYW5mKCIlZCAlZCAlZCIsICZhLCAmYiwgJmMpOwoJCWVkZ2VzLnB1c2hfYmFjayh7KGxsKSBjLHthLGJ9IH0pOwoJfQoJc29ydChhbGwoZWRnZXMpKTsKICAgIGxsIGFucz0wOwogICAgZm9yKGF1dG8maXQ6ZWRnZXMpewogICAgICAgIGxsIHU9aXQuc2Vjb25kLmZpcnN0LHY9aXQuc2Vjb25kLnNlY29uZDsKICAgICAgICBsbCB3PWl0LmZpcnN0OwogICAgICAgIHU9ZmluZCh1KSx2PWZpbmQodik7CiAgICAgICAgaWYodSE9dil7CiAgICAgICAgICAgIGFucys9dzsKICAgICAgICAgICAgdW5pdGUodSx2KTsKICAgICAgICB9CiAgICB9CiAgICBpZihjbnQ+MSl7CiAgICAgICAgY291dDw8IklNUE9TU0lCTEUiPDxlbmRsOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIHByaW50ZigiJWxsZFxuIixhbnMpOwp9CmludCB0ID0gMTsKaW50IG1haW4oKSB7CgkvL3NjYW5mKCIlZCIsICZ0KTsKCXdoaWxlICh0LS0pIHNvbHZlKCk7Cn0=