#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
struct muchie
{
int a, b, cost;
};
struct nod
{
int b, cost;
};
const int N=5e4+5, INF=2e9;
int n, m, d[N];
vector<muchie> mu;
vector<nod> ad[N];
int main()
{
fin>>n>>m;
for(int i=0;i<m;++i)
{
int x, y, z;
fin>>x>>y>>z;
mu.push_back({x, y, z});
ad[x].push_back({y, z});
}
for(int i=1;i<=n;++i)
d[i]=INF;
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, 1));
d[1]=0;
while(pq.size())
{
int x=pq.top().second;
pq.pop();
for(auto mm:ad[x])
if(d[x]+mm.cost<d[mm.b])
d[mm.b]=d[x]+mm.cost, pq.push(make_pair(d[mm.b], mm.b));
}
for(auto mm:mu)
if(d[mm.a]+mm.cost<d[mm.b])
{
fout<<"Ciclu negativ!";
return 0;
}
for(int i=2;i<=n;++i)
fout<<d[i]<<" ";
return 0;
}