#include <stdio.h>

typedef struct{
	int to;
	int from;
	int cost;
} edge;

const int INF = 10000000000;

int main(void)
{
	edge edges[10005];
	int ch[105];
	int n, m, ind;
	int i, j, a, b, v;
	int min, sum_cost;
	
	while (1){
		scanf("%d %d", &n, &m);
		
		if (n == 0 && m == 0) break;
		
		for (i = 0; i < n; i++) ch[i] = 0;
		
		for (i = 0; i < m; i++){
			scanf("%d %d %d", &a, &b, &v);
			edges[i].from = a;
			edges[i].to = b;
			edges[i].cost = v;
		}
		
		sum_cost = 0;
		ch[0] = 1;
		
		for (i = 0; i < n; i++){
			min = INF;
			
			for (j = 0; j < m; j++){
				if ( (ch[edges[j].from] == 1 && ch[edges[j].to] == 0) || (ch[edges[j].from] == 0 && ch[edges[j].to] == 1)){
					if (edges[j].cost < min){
						min = edges[j].cost;
						ind = j;
					}
				}
			}
			
			if (min == INF) break;
			
			if (ch[edges[ind].from] == 1 && ch[edges[ind].to] == 0){
				ch[edges[ind].to] = 1;
			}
			else{
				ch[edges[ind].from] = 1;
			}
			sum_cost += edges[ind].cost;
		}
		
		
		printf("%d\n", sum_cost);
	}
	
	return (0);
}