#include <iostream>
#include <climits>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main(){
	int m,n,a,b,cost;
	while(cin >> m >> n) {
		if(m==0 && n==0) break;
		int adj[110][110] = {};
		rep(i,m) {
			rep(j,m) {
				if(i==j) continue;
				adj[i][j] = INT_MAX;
			}
		}
		rep(i,n) {
			cin >> a >> b >> cost;
			adj[a][b] = cost;
			adj[b][a] = cost;
		}
		bool used[100] = {};
		int cost[100];
		fill(cost,cost+100,INT_MAX);
		cost[0] = 0;
		int ret = 0;
		while(1) {
			int v = -1;
			rep(i,m) {
				if(!used[i] && (v == -1 || cost[v] > cost[i])) {
					v = i;
				}
			}
			if(v == -1) break;
			used[v] = true;
			ret += cost[v];
			rep(i,m) {
				cost[i] = min(cost[i], adj[v][i]);
			}
		}
		cout <<  ret << endl;
	}
	return 0;
}