
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

class Bridge
{
public:
	int from;
	int to;
	int cost;

	Bridge() : from(0), to(0), cost(0) {}
	Bridge(const Bridge& b) : from(b.from), to(b.to), cost(b.cost) {}

	bool operator > (const Bridge& b) const
	{
		return cost > b.cost;
	}

	bool operator < (const Bridge& b) const
	{
		return cost < b.cost;
	}
};



int nCity, nBridge;

Bridge bridge[100];
bool city[100];

int main()
{
	while( 1 )
	{
		//load data
		cin >> nCity >> nBridge;
		if(nCity == 0 && nBridge == 0)
		{
			break;
		}

		for(int i=0; i<nBridge; i++)
		{
			cin >> bridge[i].from >> bridge[i].to >> bridge[i].cost;
		}

		for(int i=0; i<nCity; i++)
		{
			city[i] = false;
		}

		//set initial node to queue
		priority_queue<Bridge, vector<Bridge>, greater<Bridge> > que;
		for(int i=0; i<nBridge; i++)
		{
			if(bridge[i].from == 0 || bridge[i].to == 0)
			{
				que.push(bridge[i]);
			}
		}

		city[0] = true;

		//iteration
		int totalCost = 0;
		while(!que.empty())
		{
			//end check
			bool isEnd = true;
			for(int i=0; i<nCity; i++)
			{
				if(!city[i])
				{
					isEnd = false;
					break;
				}
			}

			if(isEnd)
			{
				break;
			}

			//check highest priority node
			Bridge current = que.top();
			que.pop();

			totalCost += current.cost;

			int next = 0;
			if(!city[current.to])
			{
				city[current.to] = true;
				next = current.to;
			}
		
			if(!city[current.from])
			{
				city[current.from] = true;
				next = current.from;
			}

			for(int i=0; i<nBridge; i++)
			{
				if(bridge[i].to == next || bridge[i].from == next)
				{
					if(!city[bridge[i].to] || !city[bridge[i].from])
					{
						que.push(bridge[i]);
					}
				}
			}
		}

		printf("%d\n", totalCost);
	}
}