#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int sx,sy;
int gx,gy;
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vector<int>d(n,inf);
		vector<bool>used(n);
		vvi cost(n,vi(n));
		rep(i,n)rep(j,n)cost[i][j]=inf;
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			cost[a][b]=cost[b][a]=c;
		}
		d[0]=0;
		int out=0;
		while(1){
			int v=-1;
			rep(i,n)if(!used[i]&&(v==-1||d[v]>d[i]))v=i;
			if(v==-1)break;
			used[v]=true;
			out+=d[v];
			rep(i,n)d[i]=min(d[i],cost[v][i]);
		}
		cout<<out<<endl;
		//int sum=0;
		//rep(i,n)cout<<i<<" "<<d[i]<<endl;//sum+=d[i];
		//cout<<sum<<endl;
	}
}