#include<cstdio>
#include<queue>
#include<vector>
#include<map>
#include<functional>
using namespace std;
int main()
{
	const int N = 100, INVALID_VALUE = 999;
	int cost[N][N],n,m,i,j,a,b,c,s;
	bool isUsed[N] = {false};

	while(scanf("%d%d",&n,&m),n)
	{
		for(i=0;i<n;++i)
		{
			for(j=0;j<n;++j) cost[i][j] = INVALID_VALUE;
			isUsed[i] = false;
		}
		for(i=0;i<m;++i) scanf("%d%d%d",&a,&b,&c), cost[a][b] = cost[b][a] = c;

		typedef pair<int,int>PII;	
		priority_queue<PII,vector<PII>,greater<PII> >q;
		PII p;
		q.push(make_pair(0,0));
		s = 0;
		while(!q.empty())
		{
			p = q.top(); q.pop();
			if(isUsed[p.second]) continue;
			isUsed[p.second] = true;
			s += p.first;

			for(i=0;i<n;++i)
			{
				c=cost[i][p.second];
				if(c!=INVALID_VALUE) q.push(make_pair(c,i));
			}
		}
		printf("%d\n",s);
	}
	return 0;
}