#include <iostream>
#include <algorithm>
using namespace std;

const int INF = 1e+9;
const int MAX_V = 100;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int n, m;

int prim(){
	for(int i=0 ; i < n ; i++ ){
		mincost[i] = INF;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;
	while( true ){
		int v = -1;
		for(int u = 0 ; u < n ; 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 < n ; u++ ){
			mincost[u] = min( mincost[u] , cost[v][u] );
		}
	}
	return res;
}

int main(){
	while( cin >> n >> m , n || m ){
		for(int u = 0 ; u < n ; u++ ){
			for(int v = 0 ; v < n ; v++ ){
				cost[u][v] = cost[v][u] = INF;
			}
		}
		
		for(int i=0 ; i < m ; i++ ){
			int a, b, c;
			cin >> a >> b >> c;
			cost[a][b] = cost[b][a] = c;
		}
		int ans = prim();
		cout << ans << endl;
	}
}