#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Graph {
private:
  const static int INF = 1000000; 
  std::vector<std::vector<int> > edge_;
  unsigned int size_;
public:
  Graph(unsigned int size);
  Graph& connect(int from, int to, int weight);
  std::vector<int> neighbours(int node) const;
  unsigned int size() const;
};

const int Graph::INF;

Graph::Graph(unsigned int size) {
  size_ = size;
  edge_ = std::vector<std::vector<int> > (size_, std::vector<int>(size_, INF));
}

Graph& Graph::connect(int from, int to, int weight = 1) {
  edge_[from][to] = weight;
  return *this;
}

std::vector<int> Graph::neighbours(int node) const {
  std::vector<int> neighbours;
  for (int i = 0; i < static_cast<int>(size_); ++i)
    if (edge_[node][i] < INF) neighbours.push_back(i);

  return neighbours;
}

unsigned int Graph::size() const {
  return size_;
}


Graph creat_graph(int n, int m);
int search_prim(Graph prim, int n);

vector<int> ans;
vector<vector<int>> cost(100,vector<int>(100,0));

int main(){
  int n,m;
 
  
  while(cin >> n >> m, n){     
    Graph prim = creat_graph(n,m);   /*creat graph*/
    ans.push_back(search_prim(prim,n));   /*make answer*/
  }
  
  for(int i: ans){
    cout << i << endl;
  }
  return 0;
}


 Graph creat_graph(int n,int m){
    Graph prim(n);
    int a,b,cost_t;

    for(int i=0; i<m; i++){
      cin >> a >> b >> cost_t;
      prim.connect(a,b);
prim.connect(b,a);
      cost[a][b] = cost_t;
      cost[b][a] = cost_t;
    }
    return prim;
  }
    
int search_prim(Graph prim, int n){
  int current,cost_sum;
  typedef pair<int, int> PII;
  vector<bool> frag(n,false);
  priority_queue < PII, vector < pair <int, int> >, greater < pair <int, int> > > que;
  /*cost,next_node*/
  
  cost_sum = 0;
  current = 0;
  frag[0] = true;
  for(int k=0; k<n-1; ++k){
     vector<int> near = prim.neighbours(current);

     for(int j:near){
       que.push(PII(cost[current][j],j));
     }

     while(1){
       auto top = que.top();
       que.pop();
       if(frag[top.second] == false){
	 cost_sum += top.first;
	 frag[top.second] = true;
	 current = top.second;
	 break;
	 }
     }  
  }
  return cost_sum;
}
       
       
       
     
     
     
     