#include <bits/stdc++.h>
using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
vector<pair<int, int>> *graph;
vector<int> dist;
vector<int> visited;
vector<int> nr;
queue<int> q;
int n, m;
// Bellman-Ford
int BellmanFord(int node){
q.push ( node );
dist[node] = 0;
while (!q.empty()) {
int node = q.front();
q.pop ();
visited[node] = 1;
for (auto neigh: graph[node]) {
int next = neigh.first;
int cost = neigh.second;
if (dist[node] + cost < dist[next]) {
dist[next] = dist[node] + cost;
nr[next]++;
if (nr[next] > n + 2)
return 0;
if (!visited[next]) {
q.push(next);
visited[next] = 1;
}
}
}
}
return 1;
}
int main(){
in>>n>>m;
graph = new vector <pair <int, int>> [50001];
dist.resize(n+1, INT_MAX);
visited.resize(n+1);
nr.resize(n+1);
for (int i = 1; i <= m; i++) {
int x, y, c;
in >> x >> y >> c;
graph[x].emplace_back(y, c);
}
if (!BellmanFord(1))
out << "Ciclu negativ!\n";
else {
for (int i = 2; i <= n; i++)
if (dist[i] == INT_MAX)
out<< 0 <<' ';
else
out << dist[i] << ' ';
}
in.close();
out.close();
return 0;
}