#define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)

int par[200];
int ran[200];

void init(int n){
	rep(i, n){
		par[i] = i;
		ran[i] = 0;
	}
}

int find(int x){
	if(x == par[x]){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

bool same(int x, int y){
	if(find(x) == find(y)) return true;
	else return false;
}

void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	if(ran[x] > ran[y]){
		par[y] = x;
	}else{
		par[x] = y;
		if(ran[x] == ran[y]) ran[y]++;
	}
}
PP bridge[10000];

int main(){
	int n, m, a, b, c;
	int res;
	while(cin >> n >> m && (n ||m)){
		res = 0;
		init(n);
		rep(i, m){
			cin >> bridge[i].second.first >> bridge[i].second.second >> bridge[i].first;
		}
		sort(bridge, bridge + m);
		int pos = 0;
		while(n > 1){
			PP pp = bridge[pos++];
			a = pp.second.first;
			b = pp.second.second;
			c = pp.first;
			if(!same(a, b)){
				unite(a, b);
				n--;
				res += c;
			}
		}
		cout << res << endl;
	}
	return 0;
}