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