import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class BOJ6497 {
static int N, M;
static DJS djs;
static Edge[] edges;
static class Edge implements Comparable<Edge>{
int s, e, v;
Edge(int s, int e, int v) {
this.s = s;
this.e = e;
this.v = v;
}
@Override
public int compareTo(Edge o) {
return this.v - o.v;
}
}
static class DJS{
int[] mom;
DJS(int n) {
mom = new int[n+1];
for (int i = 1; i <= n ; i++) mom[i] = i;
}
public int find(int n) {
if(n==mom[n]) return n;
return mom[n] = find(mom[n]);
}
public boolean union(int a, int b) {
a = find(a);
b = find(b);
if(a==b) return false;
mom[b] = a;
return true;
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("res/input_boj6497.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
while (true) {
st = new StringTokenizer(br.readLine().trim(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
if(N+M == 0) break;
djs = new DJS(N);
edges = new Edge[M];
int a, b, c;
long total = 0;
for (int i = 0; i < M ; i++) {
st = new StringTokenizer(br.readLine().trim(), " ");
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
edges[i] = new Edge(a,b,c);
total += c;
}
Arrays.sort(edges);
long result = 0;
for (Edge ed: edges) {
if(djs.union(ed.s, ed.e)) result += ed.v;
}
System.out.println(total-result);
}
}
}
https://www.acmicpc.net/problem/6497
총 거리에서 MST를 빼면, 그값이 절약하는 최대 값이 됨.