#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct edge{ int from,to,cost; };

int rnk[100],par[100];

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

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

void unite(int a,int b){
	a = find(a);
	b = find(b);
	if(a == b) return;
	if(rnk[a] > rnk[b]){
		par[b] = a;
	}else{
		par[a] = b;
		if(rnk[a] == rnk[b]) rnk[b]++;
	}
}

bool comp(const edge& e1,const edge& e2){
	return e1.cost < e2.cost;
}

int main(){
	int n,m;
	while(1){
		vector<edge> es;
		int sum = 0;
		cin >> n >> m;
		if(!n) break;
		init(n);
		for(int i = 0;i < m;i++){
			int a,b,c;
			cin >> a >> b >> c;
			es.push_back({a,b,c});
		}
		sort(es.begin(),es.end(),comp);
		for(int i = 0;i < m;i++){
			if(find(es[i].to) != find(es[i].from)){
				unite(es[i].to,es[i].from);
				sum += es[i].cost;
			}
		}
		cout << sum << endl;
	}
	return 0;
}