#include <bits/stdc++.h>  

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; -i)
#define INF (1 << 20)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int N, M;
int cost[101][101];
int mincost[101];
bool used[101];

int prim() {

	rep(i, N) {
		mincost[i] = INF;
		used[i] = false;
	}

	mincost[0] = 0;
	int res = 0;

	while(true) {
		int v = -1;
		rep(u, N) if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
		if(v == -1) break;
		used[v] = true;
		res += mincost[v];
		rep(u, N) mincost[u] = min(mincost[u], cost[v][u]);
	}
	return res;
}

int main() {
	while(scanf("%d %d", &N, &M), N || M) {
		rep(i, 101) rep(j, 101) cost[i][j] = INF;
		rep(i, M) {
			int a, b, Cost;
			scanf("%d %d %d", &a, &b, &Cost);
			cost[a][b] = cost[b][a] = Cost;
		}
		printf("%d\n", prim());
	}
	return 0;
}