#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int main()
{
  int n,m;
  for(;cin>>n>>m,n;)
    {
      int edge[n][n];
      memset(edge,-1,sizeof(edge));
      for(int i=0;i<m;i++)
	{
	  int a,b,cost;
	  cin>>a>>b>>cost;
	  edge[a][b]=cost;
	  edge[b][a]=cost;
	}
      bool closed[n];
      int cnt=n-1;
      int ans=0;
      memset(closed,false,sizeof(closed));
      closed[0]=true;
      priority_queue<pair<int,int>, vector<pair<int,int> > ,greater<pair<int,int> > > qu;
      for(int i=0;i<n;i++)
	{
	  if(edge[0][i]!=-1)
	    qu.push(make_pair(i,edge[0][i]));
	}
      while(true)
	{
	  int pos=qu.top().first;
	  int cost=qu.top().second;
	  qu.pop();
	  if(closed[pos])
	    continue;
	  closed[pos]=true;
	  ans+=cost;
	  cnt--;
	  if(cnt<=0)
	    break;
	  for(int i=0;i<n;i++)
	    {
	      if(!closed[i]&&edge[pos][i]!=-1)
		qu.push(make_pair(i,edge[pos][i]));
	    }
	}
      cout<<ans<<endl;
    }

}