#include <bits/stdc++.h>

using namespace std;

#define MAX 114
#define INF (1<<29)
typedef pair<int, int> pii;

vector<pii> G[MAX];

void init()
{
    for (int i = 0; i < MAX; i++) {
        G[i].clear();
    }
}

int prim()
{
    int res = 0;
    priority_queue<pii, vector<pii>, greater<pii>> Q;
    Q.push(pii(0, 0));

    bool close[MAX] = {};
    
    while (!Q.empty()) {
        pii p = Q.top(); Q.pop();
        int v = p.second;
        
        if (close[v]) continue;
        close[v] = 1;
        
        res += p.first;
        
        for (int i = 0; i < (int)G[v].size(); i++) {
            pii e = G[v][i];
            Q.push(pii(e.second, e.first));
        }        
    }    
    return res;
}

int main()
{
    int N, M, a, b, c;
    while (cin >> N >> M, N) {
        init();
        for (int i = 0; i < M; i++) {
            cin >> a >> b >> c;
            G[a].push_back(pii(b, c));
            G[b].push_back(pii(a, c));
        }
        cout << prim() << endl;
    }
    return 0;
}