#include <bits/stdc++.h>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
const int NMAX=5e4+8;
int n,m;
vector<pair<int,int> > g[NMAX];
int dist[NMAX];
int cnt[NMAX];
bool bellman(int src, int dist[]) {
for(int i=1; i<=n; i++) {
dist[i]=INT_MAX;
}
dist[src]=0;
queue<int> q; q.push(src);
while(!q.empty()) {
int nod=q.front();
q.pop();
for(const auto &[vec,w]:g[nod]) {
if(dist[vec]>w+dist[nod]) {
dist[vec]=w+dist[nod];
cnt[vec]++;
if(cnt[vec]>=n) {
return 0;
}
q.push(vec);
}
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
fin.tie(nullptr);
fout.tie(nullptr);
fin >> n >> m;
for(int i=1; i<=m; i++) {
int x,y,w;
fin >> x >> y >> w;
g[x].push_back({y,w});
}
if(!bellman(1,dist)) {
fout << "Ciclu negativ!\n";
return 0;
}
for(int i=2; i<=n; i++) {
fout << (dist[i]==INT_MAX ? 0 : dist[i]) << " ";
}
fout << "\n";
return 0;
}