#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

#define f first
#define s second

class S{
	public:
	int t;
	int v;
	S(int t,int v):t(t),v(v){}
	bool operator<(const S&a)const{
		return v>a.v;
	}
};

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		if(n==0)break;
		
		vector<pair<int,int> > load[111];
		
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			
			load[a].push_back(make_pair(b,c));
			load[b].push_back(make_pair(a,c));
		}
		
		int visit[111]={0};
		
		priority_queue<S> que;
		
		
		visit[0]=1;
		rep(i,load[0].size()){
			pair<int,int> e = load[0][i];
			que.push(S(e.f, e.s));
		}
		
		int ans=0;
		
		while(1){
			if(que.empty())break;
			
			S u = que.top();
			que.pop();
			
			if(visit[u.t]==1)continue;
			visit[u.t]=1;
			
			ans+=u.v;
			
			
			rep(i,load[u.t].size()){
				pair<int,int> e=load[u.t][i];
				que.push(S(e.f,e.s));
			}
		}
		
		printf("%d\n",ans);
	}
}