#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct E {
    int from, to, cost;
    E(int f, int t, int c) : from(f), to(t), cost(c) { }
    bool operator <(const E &x) const {
        return cost < x.cost;
    }
};

struct union_find {
    vector<int> par;
    union_find(int n) : par(n, -1) { }
    bool same(int x, int y) {
        x = root(x);
        y = root(y);
        return x == y;
    }

    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x != y) {
            par[x] += par[y]; par[y] = x;
        }
    }

    int root(int x) {
        return par[x] < 0 ? x : par[x] = root(par[x]);
    }
};

int kruskal(vector<E> edges, const int N) {
    sort(edges.begin(), edges.end());
    union_find uf(N);
    int res = 0;
    for(int i = 0; i < (int)edges.size(); i++) {
        E e = edges[i];
        if(uf.same(e.from, e.to) == false) {
            res += e.cost;
            uf.merge(e.from, e.to);
        }
    }
    return res;
}

int main() {
    for(int N, M; cin >> N >> M, N + M != 0; ) {
        vector<E> edges;
        for(int i = 0; i < M; i++) {
            int f, t, c; cin >> f >> t >> c;
            edges.push_back(E(f, t, c));
        }
        int res = kruskal(edges, N);
        cout << res << endl;
    }
}