#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n,m,a,b,c,mi,ret;
	int info[100][100];
	while(cin >> n >> m && n){
		ret = 0;
		rep(i,100)rep(j,100)info[i][j] = INT_MAX;
		for(int i=0;i<m;i++){
			cin >> a >> b >> c;
			info[a][b] = info[b][a] = c;
		}
		list<int> X,Y;
		for(int i=1;i<n;i++){
			Y.push_back(i);
		}
		X.push_back(0);
		while(!Y.empty()){
			mi = INT_MAX;
			list<int>::iterator it = X.begin();
			while( it != X.end() ){
				for(int i=0;i<n;i++){
					if( count(Y.begin(),Y.end(),i) )
						if(info[*it][i]<mi){
							a = i;
							mi = info[*it][i];
						}
				}
				++it;
			}
			X.push_back(a);
			it = Y.begin();
			while( it != Y.end() ){
				if((*it)==a){
					Y.erase(it);
					break;
				}
				++it;
			}
			ret+=mi;
		}
		cout << ret << endl;
	}
}