#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
//#include <fstream>

using namespace std;

typedef pair<int, int> P;

const int M = 100000;

int prim (vector <vector <int> > t )
{
	int size = t.size();
	vector <int> cost (size, M ); 
	vector <bool> visit (size, false );
	vector <int> path (size, -1 );

	cost[0] = 0;
	int mincost = M;
	int i,p;

	do{
		mincost = M;
		for (i = 0; i < size; ++i){
			if (!visit[i] && mincost > cost[i] ){
				mincost = cost[i];
				p = i;
			} // end if
		} // end for

		visit[p] = true;
		
		for (i = 0; i < size; ++i){
		// m[huÉ×ÚµÄ¢ém[hÅ
		// Ü¾ÜÜêÄ¢È¢m[hÌRXgÌXV
			if (!visit[i] && t[p][i] < cost[i] ){
				cost[i] = t[p][i];
				path[i] = p;
			} // end if
		} // end for

	}while ((int)count (visit.begin(), visit.end(), true ) != size );

	set <P> bridge;

	for (i = size - 1; i > 0; --i){
		int n = i;
		while (n != -1){
			P b(i, path[i] );
			bridge.insert (b );
			n = path[n];
		} // end while
	} // end for

	int res = 0;
	set <P>::iterator it = bridge.begin();
	for (; it != bridge.end(); ++it){
		res += t[(*it).first][(*it).second];		
	} // end for

	return res;
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.stellar", "r", stdin );

	string str = "";
	int n, m;

	while (getline (cin, str ) ){
		stringstream ssn(str);
		ssn >> n >> m;
		
		if (!n && !m ){
			break;
		} // end if

		vector <vector <int> > t (n, vector <int> (n, M ) );
 	
		for (int i = 0; i < m; ++i){
			int from, to, cost;
			getline (cin, str );
			stringstream ss(str);
			ss >> from >> to >> cost;
			t[from][to] = cost;
			t[to][from] = cost;
		} // end for
		
		int res = prim (t );
		cout << res << endl;	

	} // end loop
		
	return 0;
}