#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

#define INF 2000000000

using namespace std;

int main(void){
	int n,m;
	
	while(cin >> n >> m){
		if((n|m) == 0) break;
		
		int s,t,cost;
		
		int f[n][n];
		for(int i=0;i<n;i++)for(int j=0;j<n;j++){
			f[i][j] = INF;
		}
		
		for(int i=0;i<m;i++){
			cin >> s >> t >> cost;
			f[s][t] = cost;
			f[t][s] = cost;
		}
		//Minimum Spaning Tree
		vector<int> connected;
		connected.push_back(0);
		int ret = 0;
		while(connected.size() != n){
			int mini=INF,tmp,tmpindex;
			for(int i=0;i<connected.size();i++){
				for(int j=0;j<n;j++){
					if(f[connected[i]][j] < mini && 
					find(connected.begin(),connected.end(),j) == connected.end()){
						mini = f[connected[i]][j];
						tmpindex = j;
					}
				}
			}
			ret += mini;
			connected.push_back(tmpindex);
		}
		cout << ret << endl;
	}
}