#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const double EPS = 1e-10;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
    os<<'[';
    rep(i,a.size()) os<<(i?" ":"")<<a[i];
    return os<<']';
}

const int MAX_V = 100;
const int INF = 987654321;

typedef int weight;
struct edge { int from, to; weight cost; };
bool operator>(const edge & a, const edge & b) { return a.cost > b.cost; };
int V;
vector<edge> adj[MAX_V];

pair<weight, vector<edge> > prim() {
	priority_queue<edge, vector<edge>, greater<edge> > que;
	que.push((edge){-1, 0, 0});
	bool visited[MAX_V];
    fill(visited, visited + V, false);
	vector<edge> tree;
	weight wgh = 0;
	while (!que.empty()) {
		edge e = que.top();
		que.pop();
		if (visited[e.to]) continue;
		visited[e.to] = true;
		if (e.from >= 0) {
			tree.pb(e);
			wgh += e.cost;
		}
		tr(i, adj[e.to]) que.push(*i);
	}

	return mp(wgh, tree);
}

int main() {
    for(int n, m; cin >> n >> m, (n | m) != 0;) {
        rep(i, MAX_V) adj[i].clear();
        rep(i, m) {
            int a, b, cost; cin >> a >> b >> cost;
            adj[a].pb((edge){a, b, cost});
            adj[b].pb((edge){b, a, cost});
        }
        V = n;
        cout << prim().fst << endl;
    }

    return 0;
}