#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 100010, M = 200010, INF = 0x3f3f3f3f;
int n, m;
int p[N];
struct Edges{
int a, b, w;
bool operator< (const Edges &e) const
{
return w < e.w;
}
}edges[M];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int kruskal()
{
// 1. 对边权从小到大排序
sort(edges, edges + m);
for (int i = 1; i <= n; i ++ ) p[i] = i; // 初始化并查集
int res = 0, cnt = 0; // cnt 用来统计边数
// 2. 从小到大枚举每条边,边连上,长度加上,计数+1
for (int i = 0; i < m ; i ++ )
{
int a = edges[i].a, b = edges[i].b, w = edges[i].w;
a = find(a), b = find(b);
if (a != b)
{
p[a] = b;
res += w;
cnt ++ ;
}
}
if (cnt < n - 1) return INF; // 连通图的边数 = 点数 - 1
else return res;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m ; i ++ )
{
int a, b, w;
cin >> a >> b >> w;
edges[i] = {a, b, w};
}
int t = kruskal();
if (t == INF) puts("impossible");
else cout << t << endl;
return 0;
}