#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)

typedef pair<int,int> pp;

const int maxn = 110, inf = 10100;

int cost[maxn][maxn];
bool used[maxn];
int n,m,a,b;

int main()
{
	while(cin >> n >> m && (n && m))
	{
		memset(used, false, sizeof(used));
		memset(cost,inf, sizeof(cost));
		priority_queue<pp, vector<pp>, greater<pp> > que;
		que.push(pp(0, 0));

		int ans = 0;
		REP(m)
		{
			cin >> a >> b;
			cin >> cost[a][b];
			cost[b][a] = cost[a][b];
		}
		
		while(!que.empty())
		{
			pp p = que.top(); que.pop();
			if(used[p.second]) continue;
			
			ans += p.first;
			used[p.second] = true;
			
			REP(n) que.push(pp(cost[p.second][i], i ));
		}
		cout << ans << endl;
	}

	return 0;
}