#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int grid[N][N];
int st[N];
int dis[N];
int n, m;
int prim()
{
memset(st, 0, sizeof st);
memset(dis, 0x3f, sizeof dis);
int res = 0;
for (int i = 0; i < n; ++i) {
int t = -1;
for (int j = 1; j <= n; ++j) {
if (st[j] == 0 and (t == -1 or dis[t] > dis[j]))
t = j;
}
if (i and dis[t] == 0x3f3f3f3f)
return 0;
if (i) res += dis[t];
st[t] = true;
for (int j = 1; j <= n; ++j) {
dis[j] = min(dis[j], grid[t][j]);
}
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("D:/VS CODE/C++/in.txt", "r", stdin);
freopen("D:/VS CODE/C++/out.txt", "w", stdout);
#endif
cin >> n >> m;
memset(grid, 0x3f, sizeof grid);
for (int i = 0; i < m; ++i) {
int a, b, w;
cin >> a >> b >> w;
grid[a][b] = grid[b][a] = min(grid[a][b], w);
if (a == b)
grid[a][b] = 0;
}
int res = prim();
if (res) {
cout << res;
}
else {
cout << "impossible";
}
fclose(stdin);
fclose(stdout);
return 0;
}