#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

struct edge {
    int to, weight;
};

constexpr int INF = 1e9;

int main() {
    int n, m;
    while(cin >> n >> m, n) {
        vector<vector<edge>> g(n);
        for(int i=0; i<m; ++i) {
            int a, b, cost;
            cin >> a >> b >> cost;
            g[a].push_back((edge){b, cost});
            g[b].push_back((edge){a, cost});
        }

        vector<int> d(n, INF);
        vector<bool> used(n);
        d[0] = 0;
        int res = 0;
        priority_queue<P, vector<P>, greater<P>> que;
        que.push(make_pair(0, 0));
        while(!que.empty()) {
            P p = que.top();
            que.pop();
            int v = p.second;
            if(d[v] < p.first) {
                continue;
            }
            used[v] = true;
            res += d[v];
            for(auto& e : g[v]) {
                if(!used[e.to] && e.weight < d[e.to]) {
                    d[e.to] = e.weight;
                    que.push(make_pair(d[e.to], e.to));
                }
            }
        }
        cout << res << endl;
    }
}
