#include<iostream>
#include<algorithm>

using namespace std;

struct Edge {
	int a, b, cost;
};

int n, m;
int root[100], high[100];
Edge e[100000];

void init(int n) {
	for (int i = 0; i < n; i++)root[i] = i, high[i] = 1;
}

int getroot(int n){
	if (n == root[n])return n;
	return root[n] = getroot(root[n]);
}

bool issame(int a, int b) {
	return getroot(a) == getroot(b);
}

void unite(int a, int b) {
	a = getroot(a), b = getroot(b);
	if (a == b)return;
	if (high[a] > high[b])root[b] = a;
	else if (high[a] < high[b])root[a] = b;
	else if (high[a] == high[b])root[a] = b, high[a]++;
}

int main() {
	while (cin >> n >> m, n + m) {
		for (int i = 0; i < m; i++) {
			cin >> e[i].a >> e[i].b >> e[i].cost;
		}
		sort(e, e + m, [](Edge& l, Edge& r) {
			return l.cost < r.cost;
		});
		int ans = 0;
		init(n);
		for (int i = 0; i < m; i++) {
			if (!issame(e[i].a, e[i].b)) {
				unite(e[i].a, e[i].b);
				ans += e[i].cost;
			}
		}
		cout << ans << endl;
	}
	return 0;
}