/* Author: Peter Schaeffer */
#include <stdio.h>

#define SWAP(a,b) ({ struct Road* dummy=pq[a]; pq[a]=pq[b]; pq[b]=dummy; })

FILE *in;

struct Road{
	int destination;
	int value;
	struct Road *next;
};

int r, m, n, res, i, p;
struct Road roads[400000];
struct Road *first[200000], *pq[400001];

void sortdown(int x)
{
	if (2*x>p) return;
	if ((2*x+1<=p) && (pq[2*x+1]->value<pq[2*x]->value) && (pq[x]->value>pq[2*x+1]->value))
	{
		SWAP(x, 2*x+1);
		sortdown(2*x+1);
	}
	else if (pq[x]->value>pq[2*x]->value)
	{
		SWAP(x, 2*x);
		sortdown(2*x);
	}
}

void removefront()
{
	pq[1]=pq[p--];
	sortdown(1);
}

void sortup(int x)
{
	if (x<=1) return;
	if (pq[x]->value<pq[x/2]->value)
	{
		SWAP(x, x/2);
		sortup(x/2);
	}
}

void insertall(int s)
{
	while(first[s])
	{
		pq[++p]=first[s];
		sortup(p);
		first[s]=first[s]->next;
	}
}

int main()
{
	in=stdin;//fopen("dark.in", "r");
	while (1)
	{
		fscanf(in, " %d %d", &m, &n);
		if (!m) break;
		p=r=res=0; 
		for(i=0; i<m; i++) first[i]=NULL;
		for(i=0; i<n; i++)
		{
			int d1, d2, d3;
			fscanf(in, " %d %d %d", &d1, &d2, &d3);
			roads[r].destination=d2;
			roads[r].value=d3;
			roads[r].next=first[d1];
			first[d1]=roads+(r++);
			roads[r].destination=d1;
			roads[r].value=d3;
			roads[r].next=first[d2];
			first[d2]=roads+(r++);
			res+=d3;
		}
		insertall(0);
		while(p)
		{
			struct Road *front=pq[1];
			removefront();			
			if (first[front->destination])
			{
				res-=front->value;
				insertall(front->destination);
			}
		}
		printf("%d\n", res);
	}
	return 0;
}
