#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 1e3 + 10, M = 2e5 + 10;

int n, m;
int h1[N], h2[N], ed[M], ne[M], w[M], idx;
int d1[N], d2[N];

void add(int h[], int a, int b, int c) {
    ed[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

void dijkstra(int h[], int d[]) {
    memset(d, 0x3f, sizeof d1);
    priority_queue<PII, vector<PII>, greater<PII>> q;
    bool st[N] = {0};
    d[1] = 0;
    q.push({0, 1});
    while (q.size()) {
        auto [dis, u] = q.top();
        q.pop();
        if (st[u]) continue;
        st[u] = true;

        for (int i = h[u]; ~i; i = ne[i]) {
            int v = ed[i];
            if (d[u] + w[i] < d[v]) {
                d[v] = d[u] + w[i];
                q.push({d[v], v});
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    memset(h1, -1, sizeof h1);
    memset(h2, -1, sizeof h2);
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        add(h1, a, b, c);
        add(h2, b, a, c);
    }

    dijkstra(h1, d1);
    dijkstra(h2, d2);

    LL ans = 0;
    for (int i = 2; i <= n; ++i) {
        ans += d1[i] + d2[i];
    }

    cout << ans << '\n';

    return 0;
}