#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
#define INF 1 << 30
#define MAX_N 100

using namespace std;

int cost[MAX_N][MAX_N];
int mincost[MAX_N];
bool used[MAX_N];
int n;

int prim(){
  rep(i, n){
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;
  int res = 0;

  while(true){
    int v = -1;
    rep(u, n)
      if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;

    if(v == -1) break;
    used[v] = true;
    res += mincost[v];

    rep(u, n)
      mincost[u] = min(mincost[u], cost[v][u]);
  }
  return res;
}

int main(){
  int m;
  while(scanf("%d%d", &n, &m) && n+m){
    rep(i, n) rep(j, n) cost[i][j] = INF;
    rep(i, m){
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      cost[a][b] = cost[b][a] = c;
    }
    
    printf("%d\n", prim());
  }
  return 0;
}