#include <iostream>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)

#define INF 256;

int V, E;
int cost[110][110];
int mincost[110];
bool used[110];

int prim(){
    REP(i, V){
        mincost[i] = INF;
        used[i] = false;
    }
    mincost[0] = 0;
    int res = 0;

    while(true){
        int v = -1;
        REP(u, V){
            if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }

        if(v == -1) break;
        used[v] = true;
        res += mincost[v];

        REP(u, V){
            mincost[u] = min(mincost[u], cost[v][u]);
        }
    }
    return res;
}

int main(){
    while(cin >> V >> E, V+E != 0){
        REP(i, 110) REP(j, 110) cost[i][j] = INF;

        REP(i, E){
            int a, b, c;
            cin >> a >> b >> c;
            cost[a][b] = c;
            cost[b][a] = c;
        }
        cout << prim() << endl;
    }
}