#include <iostream>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;

const int INF = 1<<28;

struct edge_t {
    int from, to, cost;
    edge_t(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    bool operator<(const edge_t &o) const {
        return (cost < o.cost);
    }
    bool operator>(const edge_t &o) const {
        return (cost > o.cost);
    }
};
struct state_t {
    int v;
    int min_cost;
    state_t(int v, int min_cost) : v(v), min_cost(min_cost) {}
    bool operator<(const state_t &o) const {
        return (min_cost < o.min_cost);
    }
    bool operator>(const state_t &o) const {
        return (min_cost > o.min_cost);
    }
};
typedef vector<edge_t> vertex_t;
struct graph_t {
    vector<vertex_t> g;

    graph_t() {}

    graph_t(int vertex_count) {
        g.resize(vertex_count);
    }
    graph_t(vector<vertex_t> g) : g(g) {}

    inline vertex_t& operator[](int index) {
        return g[index];
    }

    inline void push_back(vertex_t v) {
        g.push_back(v);
    }

    int prim() {
        bool used[g.size()];
        int min_span_cost = 0;
        priority_queue< edge_t, vector<edge_t>, greater<edge_t> > q;

        fill(used, used+g.size(), false);

        used[0] = true;
        for (int i = 0; i < g[0].size(); i++) {
            q.push(g[0][i]);
        }

        while (!q.empty()) {
            edge_t e = q.top(); q.pop();
            if (used[e.to]) continue;
            used[e.to] = true;
            min_span_cost += e.cost;
            for (int i = 0; i < g[e.to].size(); i++) {
                q.push(g[e.to][i]);
            }
        }

        return min_span_cost;
    }

    void dijkstra(int s, vector<int> &min_cost) {
        min_cost.resize(g.size());
        priority_queue< state_t, vector<state_t>, greater<state_t> > q;
        
        fill(min_cost.begin(), min_cost.end(), INF);

        min_cost[s] = 0;
        q.push(state_t(s, 0));

        while (!q.empty()) {
            state_t p = q.top(); q.pop();
            int v = p.v;
            if (min_cost[v] < p.min_cost) continue;
            for (int i = 0; i < g[v].size(); i++) {
                edge_t e = g[v][i];
                if (min_cost[e.to] > min_cost[v] + e.cost)  {
                    min_cost[e.to] = min_cost[v] + e.cost;
                    q.push(state_t(e.to, min_cost[e.to]));
                }
            }
        }
    }
};

int main() {
    int n, m;
    while (cin >> n >> m) {
        if (n == 0 && m == 0) break;
        graph_t graph(n);
        for (int i = 0; i < m; i++) {
            int a, b, cost;
            cin >> a >> b >> cost;
            graph[a].push_back(edge_t(a, b, cost));
            graph[b].push_back(edge_t(b, a, cost));
        }
        cout << graph.prim() << endl;
    }
    return 0;
}