#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;
        }

        //i = 0 时,集合为空,所有dis都为无穷大,此时会选取1节点加入集合中,再用它更新自己和相邻节点的dis,
        //所以i = 0 时不能判断dis[t] 和 累加 res;

        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;
}