#include <iostream>
#include <vector>
#include <cstring>
#include <queue>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;

const int INF = 100000000;

struct edge {
    int to, cost;
    bool operator<(const edge& r)const{
        return cost>r.cost;
    }
};

// state = edge
// struct state {
//     int from, d;
// };
vector<vector<edge> > G;
int V;

bool used[111];
int prim(){
    int sum=0;
    int es=0;
    memset(used,0,sizeof(used));
    priority_queue<edge> q;
    q.push({0,0});
    while(es!=G.size()){
        edge e=q.top(); q.pop();
        if(used[e.to])continue;
        es++;
        used[e.to]=1;
        int from=e.to;
        //cout<<" "<<from<<endl;
        sum+=e.cost;
        FOR(i,0,G[from].size()){
            q.push(G[from][i]);
        }
    }
    return sum;
}

int main(){
    int m;
    while(1){
        cin>>V>>m;
        if(!(m||V))break;
        G=vector<vector<edge> >(V);
        FOR(i,0,m){
            int a,b,cost;cin>>a>>b>>cost;
            G[a].push_back({b,cost});
            G[b].push_back({a,cost});
        }
        cout<<prim()<<endl;
    }
}