[cpp]
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
const int inf=1000000;
int map[100][100];
int dis[100];
bool vis[100];
int prim(int n){
for(int i=1;i<=n;++i)
dis[i]=map[i][1],vis[i]=false;
vis[1]=true;
int ret=0;
while(1){
int min=inf,mj=-1;
for(int i=1;i<=n;++i)
if(!vis[i]&&dis[i]<min)min=dis[i],mj=i;
vis[mj]=true;
if(mj==-1)break;
ret+=min;
for(int i=1;i<=n;++i)
if(!vis[i]&&dis[i]>map[i][mj])
dis[i]=map[i][mj];
}
return ret;
}
int main(){
int n,m;
while( cin>>n>>m,n ){
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(i==j)map[i][j]=0;
else map[i][j]=inf;
while(m--){
int x,y,c; cin>>x>>y>>c;
if(c<map[x][y])
map[x][y]=map[y][x]=c;
}
cout<<prim( n )<<endl;
}
return 0;
}