#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int INF = 100000;
struct Edge { int from, to, cost; };
int dist[100];

int main() {
    int n, m;
    while (cin >> n >> m, n) {
        vector<Edge> G;
        int a, b, cost;
        for (int i=0; i<m; ++i) {
            cin >> a >> b >> cost;
            G.push_back((Edge){ a, b, cost });
            G.push_back((Edge){ b, a, cost });
        }
        int ret = 0;
        fill(dist, dist+100, INF);
        dist[0] = 0;
        while (1) {
            int k, mn = INF;
            for (int i=0; i<(int)G.size(); ++i) {
                if (dist[G[i].to] == INF && dist[G[i].from] + G[i].cost < mn) {
                    mn = dist[G[i].from] + G[i].cost;
                    k = i;
                }
            }
            if (mn == INF) break;
            dist[G[k].to] = dist[G[k].from] + G[k].cost;
            ret += G[k].cost;
        }
        cout << ret << endl;
    }
    return 0;
}