// Author: Walter Guttmann
#include <fstream>
#include <iostream>

using namespace std;

//ifstream in("dark.in");
#define in cin

const int MAXN = 1<<18;

int from[MAXN], to[MAXN], len[MAXN], perm[MAXN], pred[MAXN], rank[MAXN];

bool lencmp(int i, int j) {
  return len[i] < len[j];
}

int find(int i) {
  if (pred[i] != i)
    pred[i] = find(pred[i]);
  return pred[i];
}

void unite(int i, int j) {
  int ii = find(i);
  int jj = find(j);
  if (ii != jj)
    if (rank[ii] < rank[jj]) {
      pred[ii] = jj;
    } else {
      pred[jj] = ii;
      if (rank[ii] == rank[jj])
        ++rank[ii];
    }
}

int main() {
  int m, n;
  while (in >> m >> n) {
    if (m == 0 && n == 0) break;
    for (int i=0 ; i<n ; i++) {
      in >> from[i] >> to[i] >> len[i];
      perm[i] = pred[i] = i;
      rank[i] = 0;
    }
    sort(perm, perm+n, lencmp);
    int total = 0;
    for (int i=0 ; i<n ; i++)
      if (find(from[perm[i]]) != find(to[perm[i]]))
        unite(from[perm[i]], to[perm[i]]);
      else
        total += len[perm[i]];
    cout << total << endl;
  }
  return 0;
}

