#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50001
#define INF 1000000002
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
struct varf {int x, c;};
vector<varf> G[NMAX];
int n, start=1;
int cmin[NMAX];
int nr[NMAX];
bool cicluneg;
queue<int> C;
void citire();
void bellmanford();
void afisare();
int main()
{
citire();
bellmanford();
afisare();
return 0;
}
void citire()
{int i, x, y, cost, m;
varf vf;
fin>>n>>m;
for (i=0; i<m; i++)
{
fin>>x>>y>>cost;
vf.x=y; vf.c=cost;
G[x].push_back(vf);
}
}
void bellmanford()
{int i, x;
for (i=1; i<=n; i++) cmin[i]=INF;
cmin[start]=0;
C.push(start);
while (!C.empty() && !cicluneg)
{
x=C.front(); C.pop();
for (i=0; i<G[x].size(); i++)
if (cmin[G[x][i].x]>cmin[x]+G[x][i].c)
{
cmin[G[x][i].x]=cmin[x]+G[x][i].c;
nr[G[x][i].x]++;
if (nr[G[x][i].x]==n) cicluneg=1;
C.push(G[x][i].x);
}
}
}
void afisare()
{int i;
if (cicluneg)
fout<<"Ciclu negativ!";
else
for (i=2; i<=n; i++)
fout<<cmin[i]<<' ';
}