#include<iostream>
#include<vector>
#include<cstdio>
#include<cmath>

#define REP(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;

typedef unsigned long long ull;

int main(){
  int n,m;
  while(scanf("%d%d",&n,&m),n+m){
    int  cost[n][n];
    bool f[n];
    memset(cost, 0, sizeof(cost));
    memset(f, 0, sizeof(f));
    REP(i,m){
      int a,b,c;
      scanf("%d%d%d",&a,&b,&c);
      cost[a][b] = cost[b][a] = c;
    }
    f[0] = true;
    int ans = 0;
    REP(i,n-1){
      int m = INT_MAX;
      int ii,jj;
      for(int i=0; i<n; i++){
	for(int j=i+1; j<n; j++){
	  if(f[i] ^ f[j] && cost[i][j] != 0){
	    if(m > cost[i][j]){
	      ii = i; jj = j;
	      m = cost[i][j];
	    }
	  }
	}
      }
      f[ii] = f[jj] = true;
      ans += m;
    }

    printf("%d\n",ans);
  }
  return 0;
}