#include<stdio.h>
#include<string.h>
#include<math.h>
int a,s,d[501],f[501],g[501],h,size[101],ban[101];
int z,x,q;
int main(void)
{
	int i,j;
	while(1){
		scanf("%d %d",&a,&s);
		if(a==0&&s==0) break;
		for(i=0;i<a;i++){
			ban[i]=i;
			size[i]=1;
		}
		for(i=0;i<s;i++) scanf("%d %d %d",&d[i],&f[i],&g[i]);
		for(i=0;i<s-1;i++){
			for(j=i+1;j<s;j++){
				if(g[i]>g[j]){
					h=g[i]; g[i]=g[j]; g[j]=h;
					h=d[i]; d[i]=d[j]; d[j]=h;
					h=f[i]; f[i]=f[j]; f[j]=h;
				}
			}
		}
		q=0;
		for(i=0;i<s;i++){
			//if(d[i]>f[i]) h=d[i]; d[i]=f[i]; f[i]=h;
			z=d[i]; x=f[i];
			while(ban[z]!=z) z=ban[z];
			while(ban[x]!=x) x=ban[x];
			if(z!=x){
				q+=g[i];
				if(size[z]>size[x]) ban[x]=z;
				else ban[z]=x;
			}
		}
		printf("%d\n",q);
	}
	return 0;
}