#include <iostream>
#include <bitset>
#include <vector>
#include <queue>
#include <limits.h>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)

struct Bridge {
	int a, b, cost;
};

bool operator <(const Bridge& b1, const Bridge& b2) {
	return b1.cost < b2.cost;
}

bool operator >(const Bridge& b1, const Bridge& b2) {
	return b1.cost > b2.cost;
}

int n, m, a, b, cost;
const int MAX_N = 100;
int T[MAX_N][MAX_N];
priority_queue<Bridge, vector<Bridge>, greater<Bridge> > q;

int main() {
	while(cin >> n >> m, n | m) {
		fill(&T[0][0], &T[0][0]+MAX_N*MAX_N, INT_MAX);
		rep(i,m) {
			cin >> a >> b >> cost;
			T[a][b] = T[b][a] = cost;
		}

		q = priority_queue<Bridge, vector<Bridge>, greater<Bridge> >();
		rep(i,n) {
			if(T[0][i] != INT_MAX) {
				Bridge bdg;
				bdg.a = 0;
				bdg.b = i;
				bdg.cost = T[0][i];
				q.push(bdg);
			}
		}

		int total = 0;
		bitset<MAX_N> visited;
		visited.set(0);
		while(!q.empty()) {
			Bridge bdg = q.top();
			q.pop();
			if(visited[bdg.b]) continue;

			total += bdg.cost;
			visited.set(bdg.b);
			rep(i,n) {
				if(!visited[i] && T[bdg.b][i] != INT_MAX) {
					Bridge new_bdg;
					new_bdg.a = bdg.b;
					new_bdg.b = i;
					new_bdg.cost = T[bdg.b][i];
					q.push(new_bdg);
				}
			}
			
			if(visited.count() == n) break;
		}

		cout << total << endl;
	}
}