#include <stdio.h>
#include <stack>
#include <math.h>

using namespace std;



int main(){

	int table[100][100],n,m,sum,fixed[100],count,a,b,cost,minimum,checkTable[100],min_index;

	while(true){
		scanf("%d %d",&n,&m);
		if(n == 0 && m == 0)break;

		for(int i=0; i < n; i++){
			checkTable[i] = 0;
			for(int k=0; k < n; k++)table[i][k] = 0;
		}

		for(int i=0; i < m; i++){
			scanf("%d %d %d",&a,&b,&cost);
			table[a][b] = cost;
			table[b][a] = cost;
		}

		checkTable[0] = 1;
		fixed[0] = 0;
		count = 1;
		sum = 0;

		while(count < n){
			minimum = 1000000000;
			for(int i = 0; i < count; i++){
				for(int k=0; k < n; k++){
					if(table[fixed[i]][k] > 0 && checkTable[k] == 0 && table[fixed[i]][k] < minimum){
						minimum = table[fixed[i]][k];
						min_index = k;
					}
				}
			}

			sum += minimum;
			checkTable[min_index] = 1;
			fixed[count] = min_index;
			count++;
		}

		printf("%d\n",sum);

	}

    return 0;
}