#include <iostream>
#include <utility>
#include <queue>
#include <vector>
#include <cstring>
#include <functional>
using namespace std;
typedef pair<int,int> P;

const int MAX = 100,INF = 1000000;
int edge[MAX][MAX];

bool check(int a[],const int n){
	for(int i = 1;i < n;++i){
		if(a[i - 1] != a[i]) return true;
	}
	return false;
}

int prim(int n){
	priority_queue<P,vector<P>,greater<P> > pq;
	int done[n],cost;
	P tmp;
	memset(done,0,sizeof(done));
	for(int i = 0;i < n;++i){
		if(edge[0][i] != INF) pq.push(make_pair(edge[0][i],i));
	}
	done[0] = 1;
	cost = 0;
	while(check(done,n) && !pq.empty()){
		tmp = pq.top();
		pq.pop();
		if(!done[tmp.second]){
			cost += tmp.first;
			done[tmp.second] = 1;
			for(int i = 0;i < n;++i){
				if(edge[tmp.second][i] != INF) pq.push(make_pair(edge[tmp.second][i],i));
			}
		}
	}
	return cost;
}

int main(){
	int n,m,a,b,cost;
	while(cin >> n >> m,n){
		for(int i = 0;i < MAX;++i){
			for(int j = 0;j < MAX;++j){
				edge[i][j] = MAX;
			}
		}
		for(int i = 0;i < m;++i){
			cin >> a >> b >> cost;
			edge[a][b] = edge[b][a] = cost;
		}
		cout << prim(n) << endl;
	}
	return 0;
}