#include <cstdio>
#include <algorithm>

using namespace std;

//disjoint-set
int parent[100];

void init(int n){
  for(int i=0; i<n; i++) parent[i]=i;
}

int root(int a){
  if (parent[a] == a) return a;
  return (parent[a] = root(parent[a]));
}

bool same_set_p(int a,int b){
  return root(a) == root(b);
}

void unite(int a,int b){
  parent[root(a)] = root(b);
}

//graph
class Edge{
public:
  int cost,a,b;
  Edge(int c=0,int d=0,int e=0):cost(c),a(d),b(e){}
  bool operator< (const Edge& o) const {
    return cost < o.cost;
  }
};

//main
int n,m;
Edge graph[10000];

int main(){
  while(scanf("%d %d",&n,&m),n){
    for(int i=0; i<m; i++){
      int a,b,cost;
      scanf("%d %d %d",&a,&b,&cost);
      graph[i] = Edge(cost,a,b);
    }

    sort(graph,graph+m);

    int answer=0;
    init(n);

    for(int i=0; i<m; i++){
      Edge e = graph[i];
      if(!same_set_p(e.a,e.b)){
	answer += e.cost;
	unite(e.a,e.b);
      }
    }

    printf("%d\n",answer);
  }
}