#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;

int main() {
  int n,m;
  while(cin >> n >> m && n) {
    bool u[n];
    memset(u,0,sizeof(u));
    vector<P> v[n];
    for(int i=0; i<m; i++) {
      int x,y,z;
      cin >> x >> y >> z;
      v[x].push_back(P(y,z));
      v[y].push_back(P(x,z));
    }
    int ans=0;
    priority_queue<P,vector<P>,greater<P> > que;
    que.push(P(0,0));
    while(!que.empty()) {
      P p=que.top();que.pop();
      int x=p.second,c=p.first;
      if(u[x]) continue;
      u[x]=1;
      ans+=c;
      for(int i=0; i<v[x].size(); i++) {
        int y=v[x][i].first;
        if(!u[y]) que.push(P(v[x][i].second,y));
      }
    }
    cout << ans << endl;
  }
  return 0;
}
