#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int cost[100][100];
int mincost[100];
bool used[100];

int main() {
	int n, m;
	while (scanf("%d%d", &n, &m), n) {
		memset(cost, 0x3f, sizeof(cost));
		rep(i, m) {
			int a, b, c; scanf("%d%d%d", &a, &b, &c);
			cost[a][b] = c;
		}
		memset(mincost, 0x3f, sizeof(mincost));
		memset(used, 0, sizeof(used));
		mincost[0] = 0;
		int ans = 0;
		while (true) {
			int v = -1;
			rep(i, n) {
				if (!used[i] && (v == -1 || mincost[i] < mincost[v]))
					v = i;
			}
			if (v == -1)break;
			used[v] = true;
			ans += mincost[v];
			rep(i, n)mincost[i] = min(mincost[i], cost[v][i]);
		}
		printf("%d\n", ans);
	}
}