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

static const int INFTY = 1000001;
static const int MAX = 102;

class Edge{
public:
    int target, cost;
    Edge(int target = 0, int cost = 0) : target(target), cost(cost) {}
};

std::vector<Edge> G[MAX];
bool visited[MAX];
int parent[MAX], key[MAX];
int V, minv, min_index;

int minKey() {
    minv = INFTY;
    for (int v = 0; v < V; v++)
        if (!visited[v] && key[v] < minv)
            minv= key[v], min_index = v;
    return min_index;
}

void printMST()
{
    int sum = 0;
    for (int i = 1; i < V; i++)
        sum += key[i];
    std::printf("%d\n", sum);

}

void prim() {

    for (int i = 0; i < V; i++) {
        key[i] = INFTY;
        visited[i] = false;
    }
    key[0] = 0;
    parent[0] = -1;

    int current, target, cost;

    for (int i = 0; i < V-1; i++) {
        current = minKey();
        visited[current] = true;

        for (unsigned long v = 0; v < G[current].size(); v++) {
            target = G[current][v].target;
            cost = G[current][v].cost;
            if (!visited[target] && cost < key[target])
                parent[target] = current, key[target] = cost;
        }
    }
    printMST();

}


int main()
{
    int E, source, target, cost;
    for(; ;) {
        std::scanf("%d %d", &V, &E);
        if (V == 0 && E == 0) break;
        for (int i = 0; i < E; i++) {
            std::scanf("%d %d %d", &source, &target, &cost);
            G[source].push_back(Edge(target, cost));
            G[target].push_back(Edge(source, cost));
        }
        prim();
        for (int i = 0; i <= V; i++) G[i].clear();
    }
    return 0;
}

