#include <cstdio>
#include <iostream>
#include <string>
#define INF (1<<21)
using namespace std;

int main(){
  int i,j,n,m,s,t,cost,a[100][100],d[100],f[100],min,p,sum;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    for(i=0;i<n;i++){
      d[i] = INF;
      f[i] = 0;
      for(j=0;j<n;j++){
	a[i][j] = INF;
      }
    }

    for(i=0;i<m;i++){
      cin >> s >> t >> cost;
      a[s][t] = a[t][s] = cost;
    }

    d[0] = 0;
    while(1){
      min = INF;
      for(i=0;i<n;i++){
	if(d[i]<min && f[i] == 0){
	  min = d[i];
	  p = i;
	}
      }
      if(min == INF) break;
      f[p] = 1;
      for(i=0;i<n;i++){
	if(f[i] == 1) continue;
   	if(a[p][i] < d[i]) d[i] = a[p][i];
      }
    }
    
    sum = 0;
    for(i=0;i<n;i++){
      sum += d[i];
    }

    cout << sum << endl;
  }
  return 0;
}