#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

set<int> si;

int n, m;
int a, b, cost;
int d[110];
bool used[110];

struct edge{
	int to, cost;
	edge(int _to, int _cost) :to(_to), cost(_cost){};
};

vector<edge> G[110];

void prim(int s){
	fill(d, d + 110, INF);
	REP(i, n)used[i] = false;
	d[s] = 0;
	priority_queue<P,vector<P>,greater<P>> que;
	que.push(P(0, s));

	while (!que.empty()){
		P p = que.top(); que.pop();
		int v = p.second;

		if (used[v])continue;

		used[v] = 1;

		for (int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if (used[e.to])continue;
			if (d[e.to] > e.cost){
				d[e.to] = e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main(){
	while (cin >> n >> m&&n + m){
		REP(i, m){
			cin >> a >> b >> cost;
			G[a].push_back(edge(b, cost));
			G[b].push_back(edge(a, cost));
		}
		prim(0);
		int ans = 0;
		REP(i, n){
			ans += d[i];
		}
		cout << ans << endl;
		REP(i, n)G[i].clear();
	}
}