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

typedef pair<pair<int,int>,int> ppi;

struct Edge { int to, cost; };
vector<Edge> edges[100];

int main() {
    int n, m;
    while (cin >> n >> m, n) {
        fill(edges, edges+100, vector<Edge>());
        int a, b, cost;
        for (int i=0; i<m; ++i) {
            cin >> a >> b >> cost;
            edges[a].push_back((Edge){b, cost});
        }
        int res = 0;
        bool used[100] = {};
        priority_queue<ppi, vector<ppi>, greater<ppi> > q;
        q.push(make_pair(make_pair(0,0),0));
        while ( ! q.empty()) {
            int d = q.top().first.first;
            int now = q.top().first.second;
            int c = q.top().second;
            q.pop();
            if (used[now]) {
                continue;
            } else {
                used[now] = true;
                res += c;
                for (int i=0; i<edges[now].size(); ++i) {
                    Edge &e = edges[now][i];
                    q.push(make_pair(make_pair(d + e.cost, e.to), e.cost));
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}