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

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

int main() {
	int n, m, a, b, cost;
	while (cin >> n >> m, n) {
		vector<Edge> G;
		for (int i=0; i<m; ++i) {
			cin >> a >> b >> cost;
			G.push_back((Edge){ a, b, cost });
			G.push_back((Edge){ b, a, cost });
		}
		int ret = 0;
		bool used[100] = {};
		used[0] = true;
		while (1) {
			int k, mn = 1e5;
			for (int i=0; i<(int)G.size(); ++i) {
				if (used[G[i].from] && ! used[G[i].to] && G[i].cost < mn) {
					mn = G[i].cost;
					k = i;
				}
			}
			if (mn == 1e5) break;
			used[G[k].to] = true;
			ret += G[k].cost;
		}
		cout << ret << endl;
	}
	return 0;
}