#include<bits/stdc++.h>
using namespace std;
#define INF (1<<20)
typedef pair<int,int> P;

int N,M;
vector<P> bri[111];

int prim(){
  bool used[111];
  int ans = 0;
  memset(used,false,sizeof(used));
  priority_queue<P,vector<P>,greater<P> > que;
  que.push(P(0,0));
  while(!que.empty()){
    P p = que.top(); que.pop();
    int cost = p.first;
    int now = p.second;
    if(used[now]) continue;
    used[now] = true;
    ans += cost;
    for(int i=0;i<bri[now].size();i++){
      int next = bri[now][i].second;
      int to = bri[now][i].first;
      que.push(P(next,to));
    }
  }
  return ans;
}

int main(){
  while(cin >> N >> M ,N){
    for(int j=0;j<M;j++){
      int a,b,c;
      cin >> a >> b >> c;
      bri[a].push_back(P(b,c));
      bri[b].push_back(P(a,c));
    }
    cout << prim() << endl;
    for(int i=0;i<111;i++){
      bri[i].clear();
    }
  }
}