#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
#define p pair<int,int>
#define P pair<int,p>
using namespace std;

int Par[100], Rank[100];
int Find(int a) {
	if (Par[a] == a)return a;
	return Par[a]=Find(Par[a]);
}
void unite(int a, int b) {
	a = Find(a);
	b = Find(b);
	if (Rank[a] > Rank[b]) {
		Par[b] = a;
	}
	else if (Rank[a] < Rank[b]) {
		Par[a] = b;
	}
	else {
		Par[b] = a;
		Rank[a]++;
	}
}
int main() {
	int a, b;
	P s[500];
	while (cin >> a >> b, a | b) {
		for (int i = 0; i < a; i++) {
			Par[i] = i; Rank[i] = 0;
		}
		for (int c = 0; c < b; c++) {
			int d, e, f; scanf("%d%d%d", &d, &e, &f);
			s[c] = P(f, p(d, e));
		}
		sort(s, s + b);
		bool k[100]{};
		int sum = 0;
		for (int i = 0; i < b; i++) {
			if (Find(s[i].second.first)!=Find(s[i].second.second)) {
				unite(s[i].second.first, s[i].second.second);
				sum += s[i].first;
			}
		}
		cout << sum << endl;
	}
}