#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

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

const int INF = 1 << 24;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

typedef unsigned long long ull;

int N, M;
int cost[110][110];

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

    mincost[0] = 0;
    
    int res = 0;
    while(1){
        int u = -1;
        
        REP(i, N){
            if(!used[i] && (u == -1 || mincost[u] > mincost[i])){u = i;}
        }

        if(u == -1){break;}

        used[u] = 1;
        res += mincost[u];

        REP(i, N){
            mincost[i] = std::min(mincost[i], cost[i][u]);
        }
    }

    return res;
}

int main(){
    while(std::cin >> N >> M, N){
        REP(i, 110){
            REP(j, 110){
                cost[i][j] = INF;
            }
        }

        REP(i, M){
            int to, from, co;
            std::cin >> to >> from >> co;

            cost[to][from] = co;
            cost[from][to] = co;
        }

        std::cout << prim() << std::endl;
    }
}