#include<iostream>
#include<vector>
#include<set>
using namespace std;

#define NONE 0

int prim(int n, vector< vector<int> >v)
{
  set<int>selected;
  set<int>::iterator it;
  int mincost,mini;
  int i;
  int ans=0;

  selected.insert(0);

  while(selected.size()!=n){
    mincost=INT_MAX;
    mini=NONE;

    for(it=selected.begin();it!=selected.end();it++){
      for(i=0;i<n;i++){
	if(selected.find(i)!=selected.end())continue;
	if(v[*it][i]==NONE)continue;
	if(v[*it][i]>=mincost)continue;
	mincost=v[*it][i];
	mini=i;
      }
    }

    if(mini==NONE)break;
    selected.insert(mini);
    ans+=mincost;
  }

  return ans;
}

int main()
{
  int n,m,a,b,cost;
  int i;
  while(cin>>n>>m,n|m){
    vector< vector<int> >v(n,vector<int>(n,NONE));
    for(i=0;i<m;i++){
      cin>>a>>b>>cost;
      v[a][b]=v[b][a]=cost;
    }
    cout<<prim(n,v)<<endl;
  }
}