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

int p[100];

int find(int x){
	return x == p[x] ? x : (p[x] = find(p[x]));
}
int merge(int x,int y){
	x=find(x),y=find(y);
	if( x == y ) return 0;
	p[y] = x;
	
	return 1;
}

int main(){
	int n,m;
	while(cin >> n >> m && n){
		for(int i = 0 ; i < n ; i++) p[i] = i;
		vector< pair<int,pair<int,int> > > e;
		for(int i = 0 ; i < m ; i++){
			int a,b,c;
			cin >> a >> b >> c;
			e.push_back(make_pair(c,make_pair(a,b)));
		}
		int ans = 0;
		sort(e.begin(),e.end());
		for(int i = 0 ; i < m ; i++){
			if( merge(e[i].second.first,e[i].second.second) ){
				ans += e[i].first;
			}
		}
		cout << ans << endl;
	}
}