#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

int data[101][101];

int prim( int n )
{
	vector<int> key( n, 99999 );
	key[0] = 0;
	vector<int> pq;
	bool inQueue[101];
	for( int i = 0;i < 101;i++ ) inQueue[i] = true;
	for( int i = 0;i < n;i++ ){
		pq.push_back( key[i] );
	}

	while( !pq.empty() ){
		vector<int>::iterator it = min_element( pq.begin(), pq.end() );
		int u = distance( pq.begin(), it );
		pq.erase( it );
		inQueue[u] = false;

		for( int i = 0;i < n;i++ ){
			if( data[u][i] ){
				if( inQueue[i] ){
					int w = data[u][i];
					if( w < key[i] ){
						key[i] = w;
						pq[i] = w;
					}
				}
			}
		}
	}

	int answer = 0;
	for( int i = 0;i < n;i++ ){
		answer += key[i];
	}

	return answer;
}

int main(int argc, char const* argv[])
{
	int n,m;
	int a,b,cost;

	while( cin >> n >> m && n != 0 ){
		for( int i = 0;i < 101;i++ ){
			for( int j = 0;j < 101;j++ ){
				data[i][j] = 0;
			}
		}
		for( int i = 0;i < m;i++ ){
			cin >> a >> b >> cost;
			data[a][b] = cost;
			data[b][a] = cost;
		}

		int answer = prim( n );
		cout << answer << endl;
	}

	return 0;
}