#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>

using namespace std;

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

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

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		if(n==0)break;
		
		int to[111][111]={0};
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			
			to[a][b]=c;
			to[b][a]=c;
		}
		
		priority_queue<S> que;
		que.push(S(0,0,-1));
		
		int ans=0;
		
		int visit[111]={0};
		while(1){
			if(que.empty())break;
			
			S u = que.top();
			que.pop();
			
			if(visit[u.x]==1)continue;
			if(visit[u.bef]==1)continue;
			visit[u.x]=1;
			
			if(u.bef!=-1){
				ans += to[u.x][u.bef];
			}
			
			rep(i,n){
				if(to[u.x][i]==0)continue;
				if(visit[i]==1)continue;
				que.push( S(i ,to[u.x][i]+u.now ,u.x) );
			}
		}
		printf("%d\n",ans);
	}
}