#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

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

using namespace std;
/** Problem0180 : Stellar Performance of the Debunkey Family **/
int cost[100][100];
int mincost[100];
bool used[100];
int V, E;

int prim()
{
	for (int i=0; i<V; i++) {
		mincost[i] = INF;
		used[i] = false;
	}
	mincost[0]=0;
	int res=0;
	
	while (1) {
		int v=-1;
		for (int u=0; u<V; u++) {
			if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v=u;
		}
		
		if (v == -1) break;
		used[v] = true;
		res += mincost[v];
		
		for (int u=0; u<V; u++) {
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}
	return res;
}

int main()
{
	while (cin >> V >> E, V!=0 && E!= 0) {
		rep(i, 100) rep(j, 100) cost[i][j]=INF;
		
		rep(i, E) {
			int a, b, c;
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		cout << prim() << endl;
	}
	return 0;
}