#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

typedef pair<int, int> P;
int main()
{
	int n, m;
	while (scanf("%d %d", &n, &m), n)
	{
		vector<P> edges[100];
		for (int i = 0; i < m; ++i)
		{
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			edges[a].push_back(P(c, b));
			edges[b].push_back(P(c, a));
		}
		bool inTree[100];
		fill(inTree, inTree+n, false);
		priority_queue<P, vector<P>, greater<P> > q;
		inTree[0] = true;
		for (int i = 0; i < edges[0].size(); ++i)
			q.push(edges[0][i]);
		int ans = 0;
		while (!q.empty())
		{
			P e = q.top();
			q.pop();
			if (inTree[e.second])
				continue;

			inTree[e.second] = true;
			ans += e.first;
			for (int i = 0; i < edges[e.second].size(); ++i)
				q.push(edges[e.second][i]);
		}
		printf("%d\n", ans);
	}

	return 0;
}