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

vector<int> city(200,-1);
int getId(int i){
	if(i==city[i]) return i;
	else return getId(city[i]);
}
int main(){
	for(;;){
		int n,m;
		cin>>n>>m;
		if(n+m==0) break;
		priority_queue< pair< int, pair<int,int> >, vector< pair< int, pair<int,int> > >, greater< pair< int, pair<int,int> > > > bridge;
		for(int i=0; i<n; i++){
			city[i]=i;
		}
		for(int x,y,c;m--;){
			cin>>x>>y>>c;
			bridge.push(make_pair(c, make_pair(x,y)));
		}
		int total=0;
		for(;bridge.size();){
			int cost = bridge.top().first;
			pair<int,int> br = bridge.top().second;
			bridge.pop();
			if(getId(br.first) != getId(br.second)){
				city[getId(br.first)] = getId(br.second);
				total += cost;
			}
		}
		cout<<total<<endl;
	}
}