#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)

const int maxn=101,maxm = 5010, INF = 10010;
int cost[maxn][maxn];
int mincost[maxn];
bool used[maxn];
int n,m,a,b;

int main()
{
    while(cin >> n >> m &&(n&&m))
    {
        memset(cost,INF,sizeof(cost));
        memset(mincost,INF,sizeof(mincost));
        memset(used,false,sizeof(used));
        mincost[0] = 0;

        REP(m)
        {
            cin >> a >> b;
            cin >> cost[a][b];
            cost[b][a] = cost[a][b];
        }

        int ans = 0;
        while(true)
        {
            int minv = INF;
            REP(n)if(!used[i] &&(minv==INF || mincost[i] < mincost[minv])) minv = i;
            if(minv == INF) break;

            used[minv] = true;
            ans += mincost[minv];
            REP(n)mincost[i] = min(mincost[i], cost[minv][i]);
        }

        cout << ans << endl;
    }

    return 0;
}