#include<iostream>
#include<algorithm>
 
using namespace std;
 
const int MAX = 101;
const int INF = (1<<20);
 
int cost[MAX][MAX];
int mincost[MAX];
bool used[MAX];
int V;
 
int prim(){
  mincost[0] = 0;
  int res = 0;
 
  while(1){
 
    int v = -1;
    for(int i = 0; i < V; i++)
      if(!used[i] && (v == -1 || mincost[i] < mincost[v])) v = i;
 
    if(v == -1) break;
 
    used[v] = true;
    res+=mincost[v];

    for(int i = 0; i < V; i++)
      mincost[i] = min(mincost[i],cost[v][i]);
 
  }
  return res;
}
 
void init(){
  fill(mincost,mincost+MAX,INF);
  fill(used,used+MAX,false);
  for(int i = 0; i < MAX; i++)
    for(int j = 0; j < MAX; j++)
      cost[i][j] = INF;
}
int main(){
  int m;
  while(cin >> V  >> m && V+m){
    init();
   
    while(m--){
      int f,t,cos;
      cin >> f >> t >> cos;
      cost[f][t] = cost[t][f] = cos;
    }
    cout << prim() << endl;
  }
  return 0;
}