#include <iostream>
using namespace std;

int use[100],m,n,c;
struct poi { int n,f[100];};
struct bri { int a,b,c; };
bri b[5000],g;
poi p[100];
void check(int v) {
	int i; c++;
	for (i=0;i<p[v].n;i++) if (p[v].f[i]>-1) if (use[p[v].f[i]]==0) {use[p[v].f[i]]=1; check(p[v].f[i]);}
	}

int main() {
	int i,j,s,x,y;
    while(cin >> n >> m) {
		if (n==0) break;
		for (i=0;i<100;i++) p[i].n=0;
		for (s=0,i=0;i<m;i++) { cin >> b[i].a >> b[i].b >> b[i].c;
		 p[b[i].a].f[p[b[i].a].n]=b[i].b;  p[b[i].a].n++;
		 p[b[i].b].f[p[b[i].b].n]=b[i].a;  p[b[i].b].n++;  
		 s+=b[i].c;}
		for (i=m-1;i>0;i--) for (j=0;j<i;j++) if (b[j].c<b[j+1].c) { g=b[j]; b[j]=b[j+1]; b[j+1]=g;}
		for (i=0;i<m;i++) {
			for (j=0;j<p[b[i].a].n;j++) if (p[b[i].a].f[j]==b[i].b) { x=j; p[b[i].a].f[j]=-1; }
			for (j=0;j<p[b[i].b].n;j++) if (p[b[i].b].f[j]==b[i].a) { y=j; p[b[i].b].f[j]=-1; }
			for (j=0;j<n;j++) use[j]=0; 
			use[0]=1; c=0; check(0); 
			if (c==n) s-=b[i].c; else { p[b[i].a].f[x]=b[i].b; p[b[i].b].f[y]=b[i].a;}
		}  
		cout << s << endl;
	}
	return 0;
}