#include<algorithm>
#include<iostream>
#include<vector>

int par[100], rank[100];

void init( int n )
{
	for( int i = 0; i != n; ++i )
	{
		par[i] = i;
		rank[i] = 0;
	}

	return;
}

int find( int x )
{
	if( par[x] == 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[x] = y;
	else
	{
		par[y] = x;
		if( rank[x] == rank[y] )
			++rank[x];
	}

	return;
}

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

struct edge{ int from, to, cost; };

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

int main()
{
	while( std::cin >> n >> m, n | m )
	{
		es.clear();

		for( int i = 0; i != m; ++i )
		{
			int a, b, cost;
			std::cin >> a >> b >> cost;
			edge e = { a, b, cost };
			es.push_back( e );
		}

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

		init( n );

		int ans = 0;
		for( int i = 0; i != m; ++i )
		{
			if( !same( es[i].from, es[i].to ) )
			{
				ans += es[i].cost;
				unite( es[i].from, es[i].to );
			}
		}

		std::cout << ans << std::endl;
	}

	return 0;
}