#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 55;
const int Mod = 1000000007;
int n,m,ans;
struct node
{
int u,v,w;
}edge[maxn*maxn];
int p[maxn];
bool cmp( node a,node b )
{
return a.w < b.w;
}
int find( int x )
{
return x == p[x]?x:p[x] = find( p[x] );
}
void merge( node s )
{
int x = find( s.u );
int y = find( s.v );
if( x != y )
{
p[x] = y;
ans += s.w;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
while( scanf("%d",&n) != EOF,n )
{
scanf("%d",&m);
ans = 0;
for( int i = 0; i < m; i ++ )
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
}
sort( edge,edge+m,cmp );
for( int i = 0; i <= n; i ++ )
p[i] = i;
for( int i = 0; i < m; i ++ )
merge( edge[i] );
printf("%d\n",ans);
}
return 0;
}