#include <iostream>
#include <vector>
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
int par[104];
int ran[105];
void init(int n) {
	for (int i = 0; i<n; i++) {
		par[i] = i;
		ran[i] = 0;
	}
}
int find(int x) {
	if (par[x] == x) {
		return x;
	}
	else {
		return par[x] = find(par[x]);
	}
}
void unite(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) return;
	if (ran[x]<ran[y]) {
		par[x] = y;
	}
	else {
		par[y] = x;
		if (ran[x] == ran[y]) ran[x]++;
	}
}
bool same(int x, int y) {
	return find(x) == find(y);
}
struct edge { int s, t, cost; };
bool comp(edge& e1, edge& e2) {
	return e1.cost < e2.cost;
}
int main()
{
	int n, m;
	while (cin >> n >> m, n != 0) {
		init(n);
		edge b[10008];
		int x, y, z;
		for (int i = 0; i < m; i++) {
			cin >> x >> y >> z;
			b[i].s = x;
			b[i].t = y;
			b[i].cost = z;
		}
		int ans = 0;
		sort(b, b + m, comp);
		for (int i = 0; i < m; i++) {
			edge now = b[i];
			if (!same(now.s, now.t)) {
				unite(now.s, now.t);
				ans += now.cost;
			}
		}cout << ans<<endl;
	}
	return 0;
}