#include <iostream>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<ll,int>;

void mincammino(int N, int M, vector<int> X, vector<int> Y, vector<int> P, vector<long long>& D) {
  vector<vector<pi>> adj(N, vector<pi>());

  for (int i = 0; i < M; ++i) {
    adj[X[i]].push_back({P[i], Y[i]});
  }
  
  priority_queue<pi, vector<pi>, greater<pi>> pq;
  pq.push({0,0});

  D[0] = 0;
  for (int i = 1; i < N; ++i) {
    D[i] = -1;
  }

  while (!pq.empty()) {
    pi top = pq.top();
    ll dist = top.first;
    int node = top.second;
    pq.pop();

    for (pi other : adj[node]) {
      ll other_dist = other.first;
      int other_node = other.second;
      ll new_dist = dist + other_dist;
      if (new_dist < D[other_node] || D[other_node] == -1) {
        D[other_node] = new_dist;
        pq.push({new_dist, other_node});
      }
    }
  }
}

#ifndef EVAL
int main() {
    int N, M;
    cin >> N >> M;

    vector<int> X(M), Y(M), P(M);
    vector<long long> D(N);

    for (int i = 0; i < M; i++) {
        cin >> X[i] >> Y[i] >> P[i];
    }

    mincammino(N, M, X, Y, P, D);

    for (int i = 0; i < N; i++) {
        cout << D[i] << ' ';
    }
    cout << '\n';
}
#endif
