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

typedef pair<int, int> P;

int main()
{
	int n, m, a, b, cost;
	while (cin >> n >> m, n || m) {
		vector< queue< P > > bridge(n);
		vector<int> group(n,0);
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> cost;
			bridge[a].push(P(-cost,-b));
			bridge[b].push(P(-cost,-a));
		}
		group[0] = 1;
		priority_queue< P > pq;
		int ncost = 0;
		while (!bridge[0].empty()) {
			pq.push(bridge[0].front());
			bridge[0].pop();
		}
		for (int i = 1; i < n; i++) {
			while (group[-pq.top().second]) {
				pq.pop();
			}
			P tmp = pq.top();
			pq.pop();
			group[-tmp.second] = 1;
			ncost -= tmp.first;
			while (!bridge[-tmp.second].empty()) {
				pq.push(bridge[-tmp.second].front());
				bridge[-tmp.second].pop();
			}
		}
		cout << ncost << endl;
	}
	return 0;
}