#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair
#define PB push_back

typedef struct{
  int to,cost;
}edge;


vector<edge> E[100];

int n,m;

int kruskal(){
  int ret=0;
  set<int> V;
  V.insert(0);
  
  while(V.size()<n){
    int minc=10000,to,from;
    FOR(siter,V){
      rep(i,E[*siter].size()){
        if(V.count(E[*siter][i].to))continue;
        if(E[*siter][i].cost<minc){
          to=E[*siter][i].to;
          minc=E[*siter][i].cost;
        }
      }
    }
    ret+=minc;
    V.insert(to);
  }
  return ret;
}
  
    


main(){
  while(cin>>n>>m,n){
    
    rep(i,n)E[i].clear();
    
    rep(i,m){
      int a,b,c;
      cin>>a>>b>>c;
      E[a].PB((edge){b,c});
      E[b].PB((edge){a,c});
    }
    
    cout<<kruskal()<<endl;
  }
}