#include <iostream>
#include <fstream>
#include <queue>
#include <utility>
#include <bits/stdc++.h>
using namespace std;
const string io_file = "bellmanford";
const int MMAX = 250000;
const int NMAX = 5e4;
const int INF = 1e9;
struct muchie {
int x, y, c;
};
muchie e[MMAX + 1];
int dist[NMAX + 1];
int main() {
ifstream cin(io_file + ".in");
ofstream cout(io_file + ".out");
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++) {
cin >> e[i].x >> e[i].y >> e[i].c;
}
for(int i = 1; i <= n; i++) {
dist[i] = INF;
}
dist[1] = 0;
for(int i = 1; i <= n-1; i++) {
bool schimbat = false;
for(int j = 1; j <= m; j++) {
if(dist[e[j].x] != INF && dist[e[j].y] > dist[e[j].x] + e[j].c) {
dist[e[j].y] = dist[e[j].x] + e[j].c;
schimbat = true;
}
}
if(!schimbat) break;
}
for(int i = 1; i <= m; i++) {
if(dist[e[i].x] != INF && dist[e[i].y] > dist[e[i].x] + e[i].c) {
cout << "Ciclu negativ!\n";
return 0;
}
}
for(int i = 2; i <= n; i++) {
cout << dist[i] << ' ';
}
cout << '\n';
return 0;
}