#include <iostream>
#include <algorithm>

using namespace std;

#define rep(i,k,n) for(int i = k ; i < n ; i++ )
#define INF 10000000

bool used[101];
int mincost[101];
int cost[101][101];
int V;

int prim(){

  fill(mincost,mincost + 101,INF);
  fill(used,used+101,false);

  int re = 0;
  mincost[0] = 0;
  while(1){

    int v = -1;
    for(int i = 0 ; i < V ; i++ ){
      if(!used[i] && (v == -1 || mincost[i] < mincost[v]))v = i;
    }

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


    for(int i = 0 ; i < V ; ++i){
      if(!used[i])mincost[i] = min(mincost[i],cost[v][i]);
    }
  }
  return re;
}

int main(){
  int n;
  while(cin >> V >> n , V+n){

    fill(cost[0],cost[101],INF);
    for(int i = 0 ; i < n ; ++i ){
      int a,b,c;
      cin >> a >> b >> c;
      cost[a][b] = cost[b][a] = c;
    }
    int res = prim();
    cout << res << endl;
  }

  return 0;
}