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

struct edge{ int to, cost; };
typedef pair<int, int> P;

int m, n;

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

        int ans = 0;
        priority_queue< P, vector<P>, greater<P> > que;
        que.push(P(0, 0));
        bool visit[n];
        fill(visit, visit + n, false);
        while(!que.empty()) {
            P p = que.top(); que.pop();
            if(visit[p.second]) 
                continue;
            
            visit[p.second] = true;
            ans += p.first;
            
            for(int i = 0; i < es[p.second].size(); i++)
                que.push(P(es[p.second][i].cost, es[p.second][i].to));
        }
        cout << ans << endl;
    }
}