#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

typedef long long ll;
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e6 + 3e2;
const int M = 2600;

vector<pair<int, int>> g[M];

int n;
int d[N];
bool processed[N];

int answer(int start){
    for (int i = 1; i <= n; i++){
        d[i] = INF;
        processed[i] = false;
    }

    priority_queue<pair<int, int>> pq;
    d[start] = 0;
    pq.push({0, start});

    int ans = INF;

    while (!pq.empty()){
        int v = pq.top().second; pq.pop();
        if (processed[v]) continue;
        processed[v] = true;

        for (auto now : g[v]){
            int u = now.first, w = now.second;
            if (d[u] > d[v] + w){
                d[u] = d[v] + w;
                pq.push({-d[u], u});
            }
            if (u == start){
                ans = min(ans, d[v] + w);
            }

        }
    }

    if (ans == INF) ans = -1;

    return ans;
}

void solve(int TC) {
    int m;
    cin >> n >> m;

    for (int i = 1; i <= m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
    }

    for (int i = 1; i <= n; i++) cout << answer(i) << endl;

}


int main() {
    startTime = clock();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(false);

    bool llololcal = false;
#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    llololcal = true;
#endif

    int TC = 1;
    //cin >> TC;

    for (int test = 1; test <= TC; test++) {
        debug(test);
        solve(test);
    }

    if (llololcal) cerr << endl << "Time: " << getCurrentTime() * 1000 << " ms" << endl;

    return 0;
}
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;
using P = pair<int, int>;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<P>> g(n);
    rep(i, m)
    {
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        g[a].push_back(make_pair(b, c));
    }
    const int INF = (int)1e9;
    rep(sv, n)
    {
        vector<int> dist(n, INF);
        vector<bool> seen(n, false);
        priority_queue<P, vector<P>, greater<P>> q;
        for (P next : g[sv])
        {
            int next_pos = next.first;
            int next_dis = next.second;
            q.emplace(make_pair(next_dis, next_pos));
        }
        while (!q.empty())
        {
            P now = q.top();
            q.pop();
            int now_pos = now.second;
            int now_dis = now.first;
            if(seen[now_pos])  continue;
            seen[now_pos] = true;
            dist[now_pos] = min(dist[now_pos], now_dis);
            for (P next : g[now_pos])
            {
                int next_pos = next.first;
                int next_dis = next.second;
                if (seen[next_pos])
                {
                    continue;
                }
                q.emplace(make_pair(now_dis + next_dis, next_pos));
            }
        }
        int ans = dist[sv];
        if (ans == INF)
        {
            ans = -1;
        }
        cout << ans << endl;
    }
    return 0;
}