#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<vector<pair<int, int>>> graph;

void mincammino(int N, int M, vector<int> X, vector<int> Y, vector<int> P, vector<long long>& D) {
	
	graph.resize(N);
	for (int i = 0; i < M; i++) graph[X[i]].push_back({P[i], Y[i]});
    fill(D.begin(), D.end(), LLONG_MAX);
    
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	pq.push({0, 0});  D[0] = 0;
	
	while (!pq.empty()) {
		
		auto[w, curr] = pq.top(); pq.pop();
		
		if (D[curr] < w) continue;
		
		for (auto[w_vic, vic]: graph[curr]) {
			
			if (w + w_vic < D[vic]) {
				
				D[vic] = w + w_vic;
				pq.push({D[vic], vic});
				
			}
			
		}
		
	}
    for (int i = 0; i < N; i++) if (D[i] == LLONG_MAX) D[i] = -1;
    return;
}

#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