#include<cstdio>
#include<algorithm>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

struct edge
{
	int from, to, cost;

	edge( int from, int to, int cost )
	:	from( from ), to( to ), cost( cost )
	{}
};

int n, m;
std::vector<edge> es;

int par[100], rank[100];

void init()
{
	rep( i, n )
		par[i] = i, rank[i] = 0;

	return;
}

int find( int x )
{
	if( x == par[x] )
		return x;
	else
		return par[x] = find( par[x] );
}

void unite( int x, int y )
{
	x = find( x );
	y = find( y );
	if( x == y )
		return;

	if( rank[x] > rank[y] )
		par[y] = x;
	else
	{
		par[x] = y;
		if( rank[x] == rank[y] )
			++rank[y];
	}

	return;
}

bool same( int x, int y )
{
	return find( x ) == find( y );
}

int main()
{
	while( scanf( "%d%d", &n, &m ), n|m )
	{
		es.clear();
		init();
		rep( i, m )
		{
			int a, b, c;
			scanf( "%d%d%d", &a, &b, &c );
			es.push_back( edge( a, b, c ) );
		}

		std::sort( es.begin(), es.end(), []( const edge& e1, const edge& e2 ){ return e1.cost < e2.cost; } );

		int ans = 0;
		rep( i, es.size() )
		{
			edge& e = es[i];
			if( !same( e.from, e.to ) )
			{
				ans += e.cost;
				unite( e.from, e.to );
			}
		}

		printf( "%d\n", ans );
	}

	return 0;
}