#include <iostream>

using namespace std;
static const int INF = 99999999;
int cost[101][101];
int mincost[101];
bool used[101];
int n, m;

int prim()
{
	for(int u = 0; u < 101; ++u)
	{
		mincost[u] = INF;
		used[u] = false;
	}
	mincost[0] = 0;
	int res = 0;
	while(true)
	{
		int v = -1;
		for(int u = 0; u < n; ++u)
		{
			if(!used[u] && (v == -1 || mincost[u] < mincost[v]))
			{
				v = u;
			}
		}
		if(v == -1)
		{
			break;
		}
		used[v] = true;
		res += mincost[v];
		for(int u = 0; u < n; ++u)
		{
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}
	return(res);
}

void solve()
{
	while(cin >> n >> m, n || m)
	{
		for(int i = 0; i < 101; ++i)
		{
			for(int j = 0; j < 101; ++j)
			{
				cost[i][j] = INF;
			}
		}
		for(int i = 0; i < m; ++i)
		{
			int a, b, c;
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		cout << prim() << endl;
	}
}

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