#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
const int inf = 2000000000;
int n, m;
int d[50005], cnt[50005];
bool inq[50005];
vector<pair<int, int>> a[50005];
int main() {
f >> n >> m;
int x, y, c;
for (int i = 1; i <= m; i++) {
f >> x >> y >> c;
a[x].push_back({y, c});
}
for (int i = 1; i <= n; i++) d[i] = inf;
queue<int> q;
d[1] = 0;
q.push(1);
inq[1] = true;
while (!q.empty()) {
int nod = q.front();
q.pop();
inq[nod] = false;
for (auto p : a[nod]) {
int vecin = p.first;
int cost = p.second;
if (d[nod] + cost < d[vecin]) {
d[vecin] = d[nod] + cost;
if (!inq[vecin]) {
q.push(vecin);
inq[vecin] = true;
cnt[vecin]++;
if (cnt[vecin] >= n) {
g << "Ciclu negativ!";
return 0;
}
}
}
}
}
for (int i = 2; i <= n; i++) {
g << d[i] << " ";
}
return 0;
}