#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Bridge
{
	int cost;
	int edge[ 2 ];
};

class BridgeAsc
{
public:
	bool operator()( const Bridge& a, const Bridge& b ) const
	{
		if( a.cost < b.cost ) return true;
		return false;
	};
};

int main()
{
	int n, m;
	while( cin >> n >> m && ( n || m ) ){
		vector< Bridge > bridge;
		for( int i = 0; i < m; ++i ){
			Bridge b;
			cin >> b.edge[ 0 ] >> b.edge[ 1 ] >> b.cost;
			bridge.push_back( b );
		}
		sort( bridge.begin(), bridge.end(), BridgeAsc() );

		int city[ 100 ] = {0};
		int id = 1;
		int cntBridge = 0;
		int index = 0;
		int ans = 0;
		while( cntBridge < n - 1 ){
			Bridge b = bridge[ index ];
			int con = 0;
			int x = b.edge[ 0 ];
			int y = b.edge[ 1 ];
			if( city[ x ] != 0 && city[ x ] == city[ y ] ){
				++index;
				continue;
			}
			if( city[ x ] == 0 && city[ y ] ){
				city[ x ] = city[ y ] = id++;
			}else if( city[ x ] == 0 ){
				city[ x ] = city[ y ];
			}else if( city[ y ] == 0 ){
				city[ y ] = city[ x ];
			}else{
				for( int i = 0; i < n; ++i ){
					if( city[ i ] == city[ y ] ){
						city[ i ] = city[ x ];
					}
				}
			}
			ans += b.cost;
			++cntBridge;
			++index;
		}
		cout << ans << endl;
	}

	return 0;
}