#include<cmath>
#include<cctype>
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<utility>
#include<queue>

const int MAXN = 105;
using namespace std;
bool visited[MAXN];
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};
struct edge{
  edge(int from,int to):from(from),to(to){}
  int from,to;
};
bool operator<(edge a,edge b){
  if(a.from != b.from)return a.from < b.from;
  else return a.to < b.to;
}
typedef pair<int,edge> P;
priority_queue<pair<int,edge>,vector<P>,greater<P> > que;
int main(){
  int n,m;
  while(cin >> n >> m && n + m != 0){

    for (int i = 0; i < m; ++i){
      int a,b,c;
      cin >> a >> b >> c;
      que.push(make_pair(c,edge(a,b)));
    }
    UnionFind u(m);
    int cost = 0;
    for (int i = 0; i < n-1; ++i){
      edge top = que.top().second;
      int topCost = que.top().first;
      que.pop();
      if(u.unionSet(top.from,top.to)){
	cost += topCost;
      }
    }
    cout << cost << endl;
  }
  return 0;
}