#include <iostream>
#include <algorithm>

using namespace std;

#define MAX 110
#define MAX_E 10100

struct Edge{
  int u,v,cost;
  Edge(){}
  Edge(int u,int v,int cost) : u(u),v(v),cost(cost) {}
  bool operator < (const Edge &e)const{
    return cost < e.cost;
  }
};

int par[MAX],rank[MAX];

void init(int N){
  for(int i = 0 ; i < N ; i++){
    par[i] = i;
    rank[i] = 0;
  }
}

int find(int x){
  if(par[x] == x){
    return x;
  }
  return par[x] = find(par[x]);
}

void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(x == y){
    return;
  }
  if(rank[x] < rank[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if(rank[x] == rank[y]){
      rank[x]++;
    }
  }
}

bool same(int x,int y){
  return (find(x) == find(y));
}

Edge e[MAX_E];

int kruskal(int E){
  sort(e,e+E);
  int res = 0;
  for(int i = 0 ; i < E ; i++){
    if(!same(e[i].u,e[i].v)){
      unite(e[i].u,e[i].v);
      res += e[i].cost;
    }
  }
  return res;
}

int main(){
  int N,M,a,b,c;
  while(cin >> N >> M,N){
    init(N);
    for(int i = 0 ; i < M ; i++){
      cin >> a >> b >> c;
      e[i] = Edge(a,b,c);
    }
    cout << kruskal(M) << endl;
  }
  return 0;
}