#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n, m;
fin >> n >> m;
vector<vector<pair<int,int>>> g(n + 1);
for (int i = 0; i < m; i++) {
int x, y, c;
fin >> x >> y >> c;
g[x].push_back({y, c});
}
const int INF = INT_MAX;
vector<int> dist(n + 1, INF), cnt(n + 1, 0);
vector<bool> inQ(n + 1, false);
queue<int> q;
dist[1] = 0;
q.push(1);
inQ[1] = true;
cnt[1] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
inQ[u] = false;
for (auto [v, w] : g[u]) {
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
if (!inQ[v]) {
if (++cnt[v] >= n) {
fout << "Ciclu negativ!\n";
return 0;
}
q.push(v);
inQ[v] = true;
}
}
}
}
for (int i = 2; i <= n; i++)
fout << dist[i] << " ";
fout << "\n";
}