#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e9;
#define pii pair<int, int>
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n, m;
vector<vector<pii>> G;
vector<int> d, iq, viz;
int main() {
fin >> n >> m;
G.resize(n + 1);
d.resize(n + 1, MAX);
viz.resize(n + 1);
iq.resize(n + 1);
for(int x, y, c; fin >> x >> y >> c; G[x].push_back({y, c}));
queue<pii> q;
d[1] = 0;
viz[1] = 1;
++iq[1];
q.push({1, 0});
while(!q.empty()) {
int node = q.front().first;
viz[node] = 0;
q.pop();
for(auto [y, c] : G[node]) {
if(d[y] > d[node] + c) {
d[y] = d[node] + c;
if(!viz[y]) {
viz[y] = 1;
q.push({y, d[y]});
if(++iq[y] > n) {
fout << "Ciclu negativ!";
return 0;
}
}
}
}
}
for(int i = 2; i <= n; ++i) {
fout << (d[i] == MAX ? -1 : d[i]) << ' ';
}
}