#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
struct edge{
    int to,cost;
    edge(int a,int b):to(a),cost(b){}
    edge(){}
    bool operator>(const edge &d)const{
        return cost>d.cost;
    }
};
int main(){
    int N,M;
    while(scanf("%d%d",&N,&M),N||M){
        vector<vector<edge> >V(N);
        for(int i=0;i<M;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            V[a].push_back(edge(b,c));
            V[b].push_back(edge(a,c));
        }
        priority_queue<edge,vector<edge>,greater<edge> >Q;
        Q.push(edge(0,0));
        bool used[100];fill(used,used+100,false);
        int res=0;
        while(Q.size()){
            int to=Q.top().to,cost=Q.top().cost;
            Q.pop();
            if(used[to])continue;
            used[to]=true;
            res+=cost;
            for(int i=0;i<V[to].size();i++)Q.push(V[to][i]);
        }
        printf("%d\n",res);
    }
}