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

typedef long long lli;
typedef pair<int,int> pii;
#define rep(i,n) for(lli i=0;i<(lli)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999

int pass[200][200];


int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)break;
        
        rep(i,200)rep(j,200)pass[i][j]=INF;
        
        rep(i,m){
            int a,b,c;
            cin>>a>>b>>c;
            pass[a][b]=c;
            pass[b][a]=c;
        }
        
        bool used[200];
        int mincost[200];
        rep(i,200)mincost[i]=INF;
        rep(i,200)used[i]=false;
        
        int res=0;
        mincost[0]=0;

        while(1){
            int v=-1;
            rep(i,n){
                if( !used[i]&&(v==-1||mincost[v]>mincost[i]) )v=i;
            }
            
            if(v==-1)break;
            
            used[v]=true;
            res+=mincost[v];
            
            rep(i,n){
                mincost[i] = min( mincost[i] , pass[v][i] );
            }
        }
        cout<<res<<endl;
    }
}