#include <bits/stdc++.h>
using namespace std;
//bellman ford
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
int d[500005], vis[500005], cnt[500005];
priority_queue<int> pq;
vector<pair<int, int>> L[500001];
int main() {
int n, m;
f >> n >> m;
for (int i=1; i<=m; i++) {
int x, y, c;
f >> x >> y >> c;
L[x].push_back({y,c});
}
for (int i=1; i<=n; i++) {
d[i] = INT_MAX;
}
d[1] = 0;
vis[1] = 1;
cnt[1] = 1;
pq.push(1);
while (!pq.empty()) {
int nod = pq.top();
pq.pop();
vis[nod] = 0;
for (auto e : L[nod]) {
int vecin = e.first;
int cost = e.second;
if (d[vecin] > d[nod] + cost) {
d[vecin] = d[nod] + cost;
if (vis[vecin] == 0) {
vis[vecin] = 1;
pq.push(vecin);
cnt[vecin]++;
if (cnt[vecin] > n) {
g << "Ciclu negativ\n";
return 0;
}
}
}
}
}
for (int i = 2; i <= n; i++) {
if (d[i] == INT_MAX) g << 0 << " ";
else g << d[i] << " ";
}
return 0;
}