#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<bits//stdc++.h>
#include<functional>
using namespace std;
int parent [100];
int root(int x){
	if(parent[x]==x) return x;
	return parent[x]=root(parent[x]);
}
void unite(int x,int y){
	x=root(x);
	y=root(y);
	parent[x]=y;
}
bool same(int x,int y){
	return root(x)==root(y);
}
struct P{
	int from,to,cost;
};
bool operator<(P a,P b){
	return a.cost<b.cost;
}
bool operator>(P a,P b){
	return a.cost>b.cost;
}
int main() {
	int n,m;
	while(cin>>n>>m&&n!=0&&m!=0){
	int a,b,c;
	for(int i=0;i<n;i++)parent[i]=i;
	priority_queue<P, vector<P>, greater<P>>p;
	for(int i=0;i<m;i++){
		cin>>a>>b>>c;
		p.push(P{a,b,c});
	}
	int sum=0,d=0;
	while(d!=n-1){
		P t=p.top();p.pop();
		if(!same(t.from,t.to)){
			unite(t.from,t.to);
			d++;
			sum+=t.cost;
		}
	}
	cout<<sum<<endl;
}
}