#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

int n, m;
int field[100][100];
bool used[100];

int prim() {
	// コスト、ノード番号
	typedef pair<int, int> edge;
	int result = 0;
	fill(used, used + n, false);
	priority_queue<edge, vector<edge>, greater<edge> > que;
	que.push(make_pair(0, 0));
	while (!que.empty()) {
		edge current = que.top();
		que.pop();
		int cost = current.first;
		int pos = current.second;
		if (used[pos]) continue;
		used[pos] = true;
		result += cost;
		for (int i = 0; i < n; i++) {
			if (field[pos][i] > 0 && !used[i]) {
				que.push(make_pair(field[pos][i], i));
			}
		}
	}
	return result;
}

int main() {
	while (cin >> n >> m, n || m) {
		memset(field, 0, sizeof(field));
		for (int i = 0; i < m; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			field[a][b] = field[b][a] = c;
		}
		cout << prim() << endl;
	}
	return 0;
}