#include <bits/stdc++.h>

struct node {
    int weight;
    int to_id;

    node(int t, int w) {
        this->weight = w;
        this->to_id = t;
    }
    bool operator>(node const &rhs) const {
        if (this->weight > rhs.weight) { return true; }
        else if (this->weight < rhs.weight) { return false; }
        if (this->to_id > rhs.to_id) { return true; }
        else if (this->to_id < rhs.to_id) { return false; }
        return false;
    }
};

// Graph:
typedef std::vector<std::vector<node>> Graph;

int prim(Graph const &input) {
    std::vector<bool> used(input.size(), false);
    int used_count = 0;
    int total_weight = 0;
    std::priority_queue<node, std::vector<node>, std::greater<node>> que;

    used[0] = true; used_count++;
    for (auto const &e: input[0]) que.push(e);
    while (used_count < input.size()) {
        // std::cout << que.size() << "; " << used_count << std::endl;
        assert(!que.empty());
        node s = que.top();
        que.pop();
        if (used[s.to_id]) continue;
        // std::cout << "marked " << s.to_id << " as used." << std::endl;
        used[s.to_id] = true; used_count++;
        total_weight += s.weight;
        for (auto const &e: input[s.to_id]) que.push(e);
    }
    return total_weight;
}

int main(void)
{
    int n, m;
    while ( std::cin >> n >> m, n) {
        Graph g(n);
        for (int i = 0; i < m; i++) {
            int f, t, c;
            std::cin >> f >> t >> c;
            g[f].emplace_back(t, c);
            g[t].emplace_back(f, c);
        }

        std::cout << prim(g) << std::endl;
    }
    
    return 0;
}