#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = (1<<21);

class State{
public:
  int d,n;
  bool f;
  State(int d = 0, int n = 0, bool f = false):d(d),n(n),f(f){}
  bool operator < (const State &s) const {return d > s.d;}
};

int Dijkstra(int start/*, int goal*/, vector<vector<int> > Graph){

  int size = Graph.size();
  vector<State> d(size);
  priority_queue<State> Q;
  State u,v;

  for(int i=0;i<size;i++){
    d[i].d = INF;
    d[i].f = false;
    }
  u.d = d[start].d = 0;
  u.n = start;
  Q.push(u);

  while(!Q.empty()){
    u = Q.top();
    Q.pop();

    if(d[u.n].f) continue;
    d[u.n].f = true;

    for(int i=0;i<size;i++){
      if(!d[i].f && /*d[u.n].d +*/ Graph[u.n][i] < d[i].d){
	v = u;
	v.d = d[i].d = /*d[u.n].d +*/ Graph[u.n][i];
	v.n = i;
	Q.push(v);
      }
    }
  }
  /*return d[goal].d;*/
  int ans = 0;
  for(int i=0;i<size;i++) ans += d[i].d;
  return ans;

}

int main(){
  int n,m;
  while(cin >> n >> m && n && m){
    vector<vector<int> > G(n);
    for(int i=0;i<n;i++){
      vector<int> v(n);
      v.assign(n,INF);
      G[i] = v;
    }
    for(int i=0;i<m;i++){
      int a,b,c;
      cin >> a >> b >> c;
      G[a][b] = G[b][a] = c;
    }
    cout << Dijkstra(0,G) << endl;
  }
  return 0;
}