#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Edge {
	int to, cost;
};
bool operator >(const Edge& e1, const Edge& e2) {
	return e1.cost > e2.cost;
}

int main() {
	int n, m;
	while ( cin >> n >> m, n || m ) {
		vector< vector<Edge> > edge(n);
		for (int i = 0; i < m; ++i) {
			int a, b, cost; cin >> a >> b >> cost;
			edge[a].push_back( (Edge){ b, cost } );
			edge[b].push_back( (Edge){ a, cost } );
		}

		int ans = 0;

		priority_queue<Edge, vector<Edge>, greater<Edge> > Q;
		Q.push( (Edge){ 0, 0 } );
		vector<bool> f(n, false);
		while ( !Q.empty() ) {
			Edge e = Q.top(); Q.pop();

			if (f[e.to]) continue;
			f[e.to] = true;

			ans += e.cost;

			for (int i = 0; i < edge[e.to].size(); ++i) {
				Q.push(edge[e.to][i]);
			}
		}
		cout << ans << endl;
	}
}