#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[10010],b[10010],cost[10010];
int parent[101];
pair<int,int> bridge[10010];
void init(int N){
	for(int i=0;i<N;i++)parent[i]=i;
}
int root(int a){
	if(parent[a]==a)return a;
	return (parent[a]=root(parent[a]));
}
bool is_same_set(int a,int b){
	return (root(a)==root(b));
}
void unite(int a,int b){
	parent[root(a)]=root(b);
}

int main(){
	while(cin>>n>>m&&n>0){
		for(int i=0;i<m;i++){
			cin>>a[i]>>b[i]>>cost[i];
			bridge[i].first=cost[i];
			bridge[i].second=i;
		}
		sort(bridge, bridge+m);
		init(n);
		int ans=0;
		for(int i=0;i<m;i++){
			if(is_same_set(a[bridge[i].second],b[bridge[i].second]))continue;
			unite(a[bridge[i].second],b[bridge[i].second]);
			ans+=bridge[i].first;
		}
		cout<<ans<<endl;
	}
	return 0;
}