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

static const int INF = 99999999;

void solve()
{
	int n, m;
	while(cin >> n >> m, n || m)
	{
		vector< vector<int> > Vec(n, vector<int>(n));
		int from, to, cost;
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				Vec[i][j] = INF;
			}
		}
		for(int i = 0; i < m; ++i)
		{
			cin >> from >> to >> cost;
			Vec[from][to] = cost;
			Vec[to][from] = cost;
		}
		vector<int> dist(n);
		vector<bool> use(n);
		for(int i = 0; i < n; ++i)
		{
			dist[i] = INF;
			use[i] = false;
		}
		dist[0] = 0;
		int res = 0;
		while(true)
		{
			int v = -1;
			for(int i = 0; i < n; ++i)
			{
				if(!use[i] && (v == -1 || dist[i] < dist[v]))
				{
					v = i;
				}
			}
			if(v == -1)
			{
				break;
			}
			use[v] = true;
			res += dist[v];
			for(int i = 0; i < n; ++i)
			{
				dist[i] = min(dist[i], Vec[v][i]);
			}
		}
		cout << res << endl;
	}
}

int main()
{
	solve();
	return(0);
}