// デブンキー一家の大活躍.cpp : Defines the entry point for the console application.
//
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

bool check(bool a[], int n) {
	for (int i = 0; i < n; i++) {
		if (a[i] == false) {
			return false;
		}
	}

	return true;
}

int main(void)
{
	int result[1000], suffix = 0;

	while (1) {
		int ncity, nbridge, glaph[100][100] = { 0 };

		scanf("%d %d", &ncity, &nbridge);
		if (ncity == 0 && nbridge == 0) {
			break;
		}
		int a, b, value;
		for (int i = 0; i < nbridge; i++) {
			scanf("%d %d %d", &a, &b, &value);
			glaph[a][b] = value;
			glaph[b][a] = value;
		}

		int min, cost = 0, s;
		bool visit[100] = { false };
		visit[0] = true;

		int count = 0;
		while (check(visit, ncity) == false) {
			min = 100;
			for (int i = 0; i < ncity; i++) {
				if (visit[i] == true) { //始点
					for (int j = 0; j < ncity; j++) { //隣接点
						if (glaph[i][j] != 0 && visit[j] == false) {
							if (min > glaph[i][j]) {
								min = glaph[i][j];
								s = j;
							}
						}
					}
				}
			}

			visit[s] = true;
			cost += min;
		}

		result[suffix++] = cost;
	}

	for (int i = 0; i < suffix; i++) {
		printf("%d\n", result[i]);
	}

	return 0;
}