#include <cstdio>
#include <vector>
#include <utility>
using namespace std;
#define INF (1<<30)

int n,m;
bool linked[100] = {false};
vector<vector<pair<int,int> > > vec;

int main(){
	while(scanf("%d%d",&n,&m), n != 0){
		vec = vector<vector<pair<int,int> > >(n);
		for(int i = 0; i < m; i++){
			int a,b,cost;
			scanf("%d%d%d",&a,&b,&cost);
			vec[a].push_back(make_pair(b,cost));
			vec[b].push_back(make_pair(a,cost));
		}
		for(int i = 0; i < n; i++){
			linked[i] = false;
		}
		linked[0] = true;

		int ans = 0;
		for(int i = 1; i < n; i++){
			int mi = INF;
			int mic = INF;
			for(int j = 0; j < n; j++){
				if(linked[j]){
					int mj = INF;
					int mjc = INF;
					for(int k = 0; k < vec[j].size(); k++){
						if(vec[j][k].second < mjc && !linked[vec[j][k].first]){
							mj = vec[j][k].first;
							mjc = vec[j][k].second;
						}
					}
					if(mjc < mic){
						mi = mj;
						mic = mjc;
					}
				}
			}
			ans += mic;
			linked[mi] = true;
		}
		printf("%d\n",ans);
	}
	return 0;
}