#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main(){
  int n,m;
  while(cin >>n>>m,n||m){
    vector<pair<int,pair<int, int> > > bridge;
    bool city[101] = {false};
    int cost = 0;
    for(int i=0; i<m; i++){
      int x,y,z;
      cin >>x>>y>>z;
      bridge.push_back(make_pair(z,make_pair(x,y)));
    }
    sort(bridge.begin(),bridge.end());
    city[bridge[0].second.first] = true;
    city[bridge[0].second.second] = true;
    cost+=bridge[0].first;
    for(int i=1; i<n; i++){
      for(int j=0; j<m; j++){
	if(city[bridge[j].second.first] && (!city[bridge[j].second.second])){
	  cost+=bridge[j].first;
	  city[bridge[j].second.second] = true;
	  break;
	}
	if((!city[bridge[j].second.first]) && city[bridge[j].second.second]){
	  cost+=bridge[j].first;
	  city[bridge[j].second.first] = true;
	  break;
	}
      }
    }
    cout <<cost<<endl;
  }
  return 0;
}