#include<iostream>
#include<fstream>
#include<queue>
#include<functional>

#define X first.first
#define Y first.second
#define Z second

using namespace std;

//fstream fin("dark.in");
#define fin cin

// Union-Find mit Pfadkompression
int conn[200000];
int find(int a) {
	if(conn[a]==a)
		return a;
	int found = find(conn[a]);
	conn[a] = found;
	return found;
}
bool unite(int a, int b) {
	int pa = find(a);
	int pb = find(b);
	if(pa==pb)
		return false;
	conn[pb] = pa;
	return true;
}

typedef pair<pair<int,int>, int> road;
struct roadcmp {
	bool operator()(const road& a, const road& b) {
		return a.Z > b.Z;
	}
};

int handle(int m, int n) {
	for(int i = 0; i < m; i++) conn[i] = i;
	priority_queue<road, vector<road>, struct roadcmp> pq;
	int oldcost = 0, newcost=0;
	for(int i = 0; i < n; i++)	{
		road r;
		fin >> r.X >> r.Y >> r.Z;
		pq.push(r);
		oldcost += r.Z;
	}
	int roads = 0;
	while(roads < m-1) {
		road r = pq.top();
		pq.pop();
		if(find(r.X)!=find(r.Y)) {
			unite(r.X, r.Y);
			newcost += r.Z;
			roads++;
		}
	}
	return oldcost-newcost;
/*	for(int i = 0; i < n; i++)	{
		road r = pq.top();
		pq.pop();
		cout << r.X << r.Y << r.Z << endl;
	} */
}

int main() {
	int n,m;
	while(1) {
		fin >> m >> n;
		if(n==0 && m==0) break;
		cout << handle(m, n) << endl;
	}
		
	return 0;
}
