#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

class UnionFind{
	int n;
	vector<int> par;
	vector<int> rank;
public:
	UnionFind(){ }
	UnionFind(int size){ reset(size); }

	void reset(int size){
		n = size;
		par.resize(size);
		rank.resize(size);
		for(int i = 0; i < size; i++) par[i] = i,rank[i] = 0;
	}

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

	void unite(int x,int y){
		x = find(x),y = find(y);
		if(x == y) return ;
		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
		n--;
	}

	bool same(int x,int y){return find(x) == find(y);}

	int size() {return n;}
};

int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		UnionFind u(n);
		vector<pair<int,Pii> > v;
		FOR(i,m){
			int a,b,c; cin>>a>>b>>c;
			v.push_back(make_pair(c,Pii(a,b)));
		}
		int ans = 0;
		sort(v.begin(),v.end());
		FOR(i,m){
			if(u.size() == 1) break;
			int cost = v[i].first;
			Pii p = v[i].second;
			if(!u.same(p.first,p.second)){
				ans += cost;
				u.unite(p.first,p.second);
			}
		}
		cout << ans << endl;
	}
	return 0;
}