#include <iostream>
#include <queue>
#define MAX 5010
using namespace std;

class edge{
public:
    int start;
    int goal;
    int cost;
    edge(){
        start = 0;
        goal = 0;
        cost = MAX;
    }
};
bool operator< (const edge &edge1, const edge &edge2){
    return edge1.cost < edge2.cost;
}
bool operator> (const edge &edge1, const edge &edge2){
    return edge1.cost > edge2.cost;
}

int P[101];
void init(int N){
    for(int i=0;i<=N;i++)
        P[i] = i;
}
int root(int a){
    if(P[a]==a)
        return a;
    return(P[a] = root(P[a]));
}
bool is_same_set(int a, int b){
    return root(a) == root(b);
}
void unit(int a, int b){
    P[root(a)] = root(b);
}

int main(){
    int n,m;
    while(cin >> n >> m && n!=0){
        init(n);
        int total = 0;
        vector<edge> v;
        for(int i=0;i<m;i++){
            edge tmp;
            cin >> tmp.start >> tmp.goal >> tmp.cost;
            v.push_back(tmp);
        }
        priority_queue<edge, vector<edge>, greater<edge> > p(v.begin(),v.end());
        for(int i=0;i<m;i++){
            edge tmp = p.top();
            p.pop();
            if(!is_same_set(tmp.start,tmp.goal)){
                total += tmp.cost;
                unit(tmp.start,tmp.goal);
            }
        }
        cout << total << endl;
    }
}