#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <set>
#define INF 1e8

using namespace std;
int main(){
  int n,m;
  while(cin>>n>>m,n||m){
    vector<vector<int> > g(n);
    for(int i=0;i<n;++i){
      g[i].reserve(n);
      for(int j=0;j<n;++j) g[i][j]=INF;
    }
    for(int i=0,a,b,cost;i<m;++i){
      cin>>a>>b>>cost;
      g[a][b]=cost;
      g[b][a]=cost;
    }
    int ans=0;
    set<int> v;
    v.insert(0);
    while(v.size()!=n){
      int min_cost=INF,to=0;
      for(set<int>::iterator vit=v.begin();vit!=v.end();++vit){
        for(int i=0;i<n;++i){
          if(v.find(i)==v.end()&&g[*vit][i]<min_cost){
            to=i;
            min_cost=g[*vit][i];
          }
        }
      }
      ans+=min_cost;
      v.insert(to);
    }
    cout<<ans<<endl;
  }
  return 0;
}