// Problem: E - Come Back Quickly
// Contest: AtCoder - AtCoder Beginner Contest 191
// URL: https://atcoder.jp/contests/abc191/tasks/abc191_e
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define mod 1000000007

// templates
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define po(x, y) fixed << setprecision(y) << x
#define ss(s) scanf(" %[^\n]%*c", s)
#define sc(n) scanf("%d", &n)
#define sl(n) scanf("%lld", &n)
#define ps(s) printf("%s\n", s)
#define pr(n) printf("%d\n", n)
#define pl(n) printf("%lld\n", n)
#define prs(n) printf("%d ", n)
#define pls(n) printf("%lld ", n)
using namespace std;

const ll inf = (ll)1e15 + 10;
const int N = (int)2e3 + 10;
ll dist[N][N];
ll bk[N][N];
vector<pair<int, ll>> adj[N];
vector<ll> ans;
void bfs(int node) {
  queue<pair<int, ll>> q;
  dist[node][node] = 0;
  q.push({node, 0});
  while (sz(q)) {
    auto t = q.front();
    q.pop();
    for (auto child : adj[t.F]) {
      if (child.S + t.S < dist[node][child.F]) {
        dist[node][child.F] = child.S + t.S;
        q.push({child.F, child.S + t.S});
      }
    }
  }
}
void solve() {
  int n, m;
  sc(n), sc(m);
  ll c;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dist[i][j] = inf;
      bk[i][j] = inf;
    }
  }
  vector<array<ll, 3>> ar;
  for (int i = 1; i <= m; i++) {
    int u, v;

    sc(u), sc(v), sl(c);
    bk[u][v] = c;
    ar.push_back({u, v, c});
  }
  sort(all(ar));
  for (int i = 0; i < m; i++) {
    int u = ar[i][0], v = ar[i][1];
    ll c = ar[i][2];
    if (i && u == ar[i - 1][0] && v == ar[i - 1][1]) {
      continue;
    }
    adj[u].push_back({v, c});
  }
  for (int i = 1; i <= n; i++) {
    bfs(i);
  }
  // printf("swapno\n");
  for (int i = 1; i <= n; i++) {
    ll mn = inf;
    for (auto p : adj[i]) {
      mn = min(mn, p.S + dist[p.F][i]);
    }
    if (mn == inf) {
      pr(-1);
    } else {
      pl(mn);
    }
  }
}

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  int t = 1;
  // sc(t);
  while (t--) {
    solve();
  }
  //	cerr << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
  return 0;
}
#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

template<typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G){
    const int n = G.size();
    vector<T> d(n, numeric_limits<T>::max());
    vector<int> b(n, -1);
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
    d[s] = 0;
    q.emplace(d[s], s);
    while (!q.empty()){
        pair<T, int> p = q.top();
        q.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (auto& e : G[v]){
            int u = e.first;
            T c = e.second;
            if (d[u] > d[v] + c){
                d[u] = d[v] + c;
                b[u] = v;
                q.emplace(d[u], u);
            }
        }
    }
    return d;
}

using edge = pair<int, int>;

int N, M;
vector<vector<edge>> G, transG;

void input(void){
	cin >> N >> M;
	G.resize(N);
	transG.resize(N);
	for (int i = 0; i < M; ++i){
		int A, B, C;
		cin >> A >> B >> C;
		--A, --B;
		G[A].emplace_back(B, C);
		transG[B].emplace_back(A, C);
	}
}

int solve(int v){
	const int INF = numeric_limits<int>::max();
	auto mindist = dijkstra(v, G);
	int ans = INF;
	for (edge e : transG[v]){
		if (mindist[e.first] == INF)
			continue;
		ans = min(ans, e.second + mindist[e.first]);
	}
	if (ans == INF) return -1;
	return ans;
}

int main(void){
	input();
	for (int i = 0; i < N; ++i)
		cout << solve(i) << endl;
	return 0;
}