#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 10010 ;
const int maxm = 100010 ;
const int inf = 0x3f3f3f3f ;
int flag[maxn] ;
int F[maxn] ;
int n , m ;
struct node
{
int u , v , w ;
bool operator < (const struct node tmp)const
{
return tmp.w < w ;
}
}edge[maxm] ;
int find(int x)
{
if(x == F[x])return F[x] ;
return F[x] = find(F[x]) ;
}
void kruskal()
{
int ans = 0 ;
for(int i = 1;i <= m;i++)
{
int fu = find(edge[i].u);
int fv = find(edge[i].v);
if(flag[fv] && flag[fu])
continue ;
ans += edge[i].w ;
F[fv] = fu ;
if(flag[fv] || flag[fu] || fv == fu)
flag[fu] = 1 ;
}
cout<<ans<<endl;
}
int main()
{
while(scanf("%d%d" , &n , &m) && (n + m))
{
memset(flag , 0 , sizeof(flag)) ;
for(int i = 0;i <= n;i++)
F[i] = i ;
for(int i = 1;i <= m;i++)
scanf("%d%d%d" , &edge[i].u , &edge[i].v , &edge[i].w) ;
sort(edge + 1, edge + 1 + m) ;
kruskal();
}
return 0 ;
}