#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#define _w_ 1
const int INF=1e9;
int cost[101][101];
int mincost[101];
bool used[101];
int V;

int prim(){
	for(int i = 0; i < V; ++i){
		mincost[i]=INF;
		used[i]=false;
	}
	mincost[0]=0;
	int res=0;
	while(_w_){
		int v=-1;
		for(int u=0;u<V;++u){
			if(!used[u] && (v == -1 || mincost[u] < mincost[v]))v=u;
		}
		if(v==-1)break;
		used[v] = true;
		res += mincost[v];
		for(int u = 0; u < V; ++u){
			mincost[u] = std::min(mincost[u], cost[v][u]);
		}
	}
	return res;
}

int main(){
	int n, m;
	while(scanf("%d %d",&n, &m) , n+m){
		for(int i=0;i<101;++i){
			for(int j=0;j<101;++j){
				cost[i][j]=INF;
			}
		}

		for(int i=0;i<m;++i){
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			cost[a][b] = c, cost[b][a] = c;
		}
		V = n;
		printf("%d\n",prim());

	}
}