#include <iostream>
#include <vector>
#include <climits>
using namespace std;

int main(){
  int br[100][100];
  bool f[100];
  int tr[100];
  int n,m;

  while(1){
    cin>>n>>m;
    if(n==0&&m==0) break;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++)
	br[i][j] = -1;
      tr[i]=INT_MAX;
      f[i]=0;
    }    
    for(int k=0;k<m;k++){
      int a,b,cost;
      cin>>a>>b>>cost;
      br[a][b]=cost;
      br[b][a]=cost;
    }
    int comin=0,r,trmin;
    tr[0] = 0;

    for(int k=0;k<n;k++){
      trmin=INT_MAX;
      for(int i=0;i<n;i++)
	if(f[i]==0 && trmin>tr[i]) r=i,trmin=tr[i];
      //cout << trmin<<endl;
      comin+=trmin;
      f[r]=1;
      for(int i=0;i<n;i++){
	if(f[i]==0 && tr[i]>br[r][i] && br[r][i]!=-1) tr[i]=br[r][i];
      }
    }
    cout <<comin<<endl;
  }
  
  return 0;
}