public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int totalNos, totalArestas;
		int no1, no2, peso;
		List<Node> grafo;
		UnionFindSet unionFind;
		
		while (true) {
			st = new StringTokenizer(br.readLine());
			totalNos = Integer.parseInt(st.nextToken());
			totalArestas = Integer.parseInt(st.nextToken());
		
			if (totalNos == 0 & totalArestas == 0)
				break;
		
			int custoTotal = 0;
			int custoPath = 0;
			int arestasPath = 0;
		
			grafo = new ArrayList<Node>();
			unionFind = new UnionFindSet(totalNos + 1);
		
			for (int i = 0; i < totalArestas; i++) {
				st = new StringTokenizer(br.readLine());
				no1 = Integer.parseInt(st.nextToken());
				no2 = Integer.parseInt(st.nextToken());
				peso = Integer.parseInt(st.nextToken());
				custoTotal += peso;
				grafo.add(new Node(no1, no2, peso));
			}
			
			Collections.sort(grafo);

			Node nodeAtual;

			for (int i = 0; i < totalArestas | arestasPath < totalNos - 1; i++) {
				nodeAtual = grafo.get(i);
				if (!unionFind.isUnion(nodeAtual.no1, nodeAtual.no2)) {
					arestasPath++;
					custoPath += nodeAtual.aresta;
					unionFind.union(nodeAtual.no1, nodeAtual.no2);
				}
			}

			System.out.println(custoTotal - custoPath);
		}

		br.close();
	}