#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define vll vector<ll>
using namespace std;
inline void sol() {
  //
  ll n, m;
  cin >> n >> m;
  vector<pll> adj[n + 1];
  for (int i = 0; i < m; i++) {
    ll u, v, c;
    cin >> u >> v >> c;
    adj[u].push_back({c, v});
  }
  for (int i = 1; i <= n; i++)
    sort(adj[i].begin(), adj[i].end());
  for (int start = 1; start <= n; start++) {
    vll costs(n + 1, 1e12);
    priority_queue<pll, vector<pll>, greater<pll>> pq;
    for (auto &p : adj[start]) {
      pq.push(p);
    }
    while (not pq.empty()) {
      auto p = pq.top();
      pq.pop();
      if (costs[p.second] <= p.first)
        continue;
      costs[p.second] = p.first;
      for (auto &q : adj[p.second]) {
        ll next = costs[p.second] + q.first;
        pq.push({next, q.second});
      }
    }
    if(costs[start] == 1e12)
      cout << "-1\n";
    else 
      cout << costs[start] << endl;
  }
}
 
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
 
  sol();
 
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
typedef long double ld;
using P = pair<int,int>;
const int INF=1001001001;
const int mod =1e9+7;

struct edge{
  int to;
  ll w;
  edge(int to,ll w):to(to),w(w){}
};
using Graph=vector<vector<edge>>;

int n,m;

auto dijkstra(int s,Graph G){
  vector<ll>dist(n,INF);
  priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>q;
  for(auto u:G[s]){
    if(chmin(dist[u.to],u.w)){
      q.push(P(u.w,u.to));
    }
  }
  while(!q.empty()){
    int v=q.top().second;
    ll d=q.top().first;
    if(v==s){return d;}
    q.pop();
    if(d>dist[v]){continue;}
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        q.push({dist[e.to],e.to});
      }
    }
  }
  return -1ll;
}

void solve(){
  cin>>n>>m;
  Graph G(n);
  vector<ll>r(n,INF);
  rep(i,m){
    ll a,b,c;
    cin>>a>>b>>c;
    a--;b--;
    G[a].push_back(edge(b,c));
  }
  rep(i,n){
    ll ans=dijkstra(i,G);
    cout<<(ans==INF?-1:ans)<<endl;
  }
}
int main() {
  solve();
  return 0;
}