#include <cstdio>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)

typedef long long ll;
//struct edge {int to, cost;};
typedef pair<int, int> P;
typedef complex<double> C;

struct edge {
    int from, to, cost;
    edge() {}
    edge(int f, int t, int c) {
        from = f; to = t; cost = c;
    }
};

bool cmp (const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

int INF = 100000000;
const int MAX_V = 100000;
const int MAX_E = 100000;

int uni[MAX_V];
edge es[MAX_E];
int V, E;

void init_uf(int n) {
    REP(i, n) uni[i] = -1;
}

int root(int x) {
    if (uni[x] < 0) return x;
    else {
        return uni[x] = root(uni[x]);
    }
}

bool same(int x, int y) {
    return root(x) == root(y);
}

void unite(int x, int y) {
    x = root(x);
    y = root(y);

    if (x == y) return;

    if (uni[x] > uni[y]) swap(x, y);

    uni[x] += uni[y];
    uni[y] = x;
}

int kruskal() {
    sort(es, es + E, cmp);
    init_uf(V);
    int res = 0;
    REP(i, E) {
        edge e = es[i];
        if (!same(e.from, e.to)) {
            unite(e.from, e.to);
            res += e.cost;
        }
    }
    return res;
}

void solve() {
    printf("%d\n", kruskal() );
}

int main() {
    while (true) {
        scanf("%d%d", &V, &E);
        if (!V && !E) return 0;
        REP(i, E) {
            int f, t, c;
            scanf("%d%d%d", &f, &t, &c);
            es[i] = edge(f, t, c);
        }
        solve();
    }
}