#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

int cost[111][111];
int minc[111];
bool used[111];
int n, m;

int prim() {
    for (int i = 0; i < n; i++) {
        minc[i] = INF;
        used[i] = false;
    }
    minc[0] = 0;
    int res = 0;
    while (1) {
        int v = -1;
        for (int u = 0; u < n; u++) {
            if (!used[u] && (v == -1 || minc[u] < minc[v])) v = u;
        }
        if (v == -1) break;
        used[v] = true;
        res += minc[v];
        for (int u = 0; u < n; u++) {
            minc[u] = min(minc[u], cost[v][u]);
        }
    }
    return res;
}

int main() {
    while (cin >> n >> m && (n != 0 || m != 0)) {
        for (int i = 0; i < 111; i++) {
            for (int j = 0; j < 111; j++) {
                cost[i][j] = INF;
            }
        }
        for (int i = 0, a, b, c; i < m; i++) {
            cin >> a >> b >> c;
            cost[a][b] = cost[b][a] = c;
        }
        int ans = prim();
        cout << ans << endl;
    }
}
