#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int p[110];
typedef pair<int,pair<int,int> >P;
priority_queue<P,vector<P>,greater<P> >Q;
int main(){
  int n,m;
  while(cin>>n>>m&&n+m){
    for(int i=0;i<m;i++){
      int a,b,c;
      cin>>a>>b>>c;
      Q.push(make_pair(c,make_pair(a,b)));
    }
    for(int i=0;i<n;i++)p[i]=i;
    int ans=0;
    while(!Q.empty()){
      int a=Q.top().second.first,b=Q.top().second.second;
      int cost=Q.top().first;Q.pop();
      int x=a,y=b;
      while(p[x]!=x)x=p[x];
      while(a!=x)a=p[a],p[a]=x;
      while(p[y]!=y)y=p[y];
      while(b!=y)b=p[b],p[b]=y;
      if(x!=y){
	ans+=cost;
	p[x]=y;
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}