#include <iostream>
#include <map>
#include <set>
using namespace std;

#define in cin
//#include <fstream>
//ifstream in("2515.txt");

map<int, map<int, int> > net;
set<int> networking, nodos;

int masCercano(int& length) {
    int minLength = 101, masCerca;
    map<int, int> nodo;
    for (set<int>::iterator it = networking.begin(); it != networking.end(); it++) {
        nodo = net[*it];
        for (set<int>::iterator itS = nodos.begin(); itS != nodos.end(); itS++)
            if (nodo.find(*itS) != nodo.end() && nodo[*itS] < minLength) {
                minLength = nodo[*itS];
                masCerca = *itS;
            }
    }
    length = minLength;
    return masCerca;
}

int main() {
    int P, R, i, j, length, distancia;
    while (in >> P >> R && P != 0) {
        net.clear();
        for (int k = 0; k < R && in >> i >> j >> length; k++)
            if (net[i].find(j) == net[i].end())
                net[i][j] = net[j][i] = length;
            else
                net[i][j] = net[j][i] = min(net[i][j], length);
        networking.clear();
        networking.insert(P);
        distancia = 0;
        for (int k = 1; k < P; k++)
            nodos.insert(k);
        while (!nodos.empty()) {
            int minimo = masCercano(length);
            distancia += length;
            networking.insert(minimo);
            nodos.erase(minimo);
        }
        cout << distancia << endl;
    }
    //getchar();
    return 0;
}

