#include <bits/stdc++.h>
using namespace std;

const int INF = 1<<29;

const int MAX_N = 100, MAX_M = 100;

int n, m;
int G[MAX_N][MAX_N];
int d[MAX_N];
bool visited[MAX_N];

int main(){
    while(scanf("%d%d", &n, &m) && (n||m)){
        for(int x = 0; x < n; x++){
            for(int y = 0; y < n; y++){
                if(x - y) G[x][y] = INF;
                else G[x][y] = 0;
            }
        }
        for(int x = 0; x < m; x++){
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            G[a][b] = c;
            G[b][a] = c;
        }
        fill(d, d+n, INF);
        fill(visited, visited+n, false);
        d[0] = 0;
        visited[0] = true;
        int ans = 0;
        int a = 0;
        for(int x = 0; x < n; x++){
            int v = -1;
            for(int y = 0; y < n; y++){
                if(visited[y]) continue;
                d[y] = min(d[y], G[a][y]);
                if(v == -1 || d[y] < d[v]){
                    v = y;
                }
            }
            if(v == -1) break;
            visited[v] = true;
            ans += d[v];
            a = v;
        }
        printf("%d\n", ans);
    }
    return 0;
}