#include <fstream>
#include <vector>
#include <queue>
#define N 50005
#define inf 1e9
using namespace std;
ifstream fin ("bellmanford.in");
ofstream fout("bellmanford.out");
int n, dist[N], viz[N];
vector <pair <int, int> > G[N];
queue <int> q;
bool BF()
{
for(int i = 2; i <= n; ++i) dist[i] = inf;
q.push(1);
while(!q.empty())
{
int nod = q.front();
q.pop();
viz[nod]++;
if(viz[nod] == n) return 0;
for(int i = 0; i < G[nod].size(); ++i)
{
int to = G[nod][i].first;
int cost = G[nod][i].second;
if(dist[to] > dist[nod] + cost)
{
dist[to] = dist[nod] + cost;
q.push(to);
}
}
}
return 1;
}
int main()
{
int m;
fin >> n >> m;
for(int i = 1, x, y, c; i <= m; ++i)
{
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
fin.close();
if(!BF()) fout << "Ciclu negativ!";
else
for(int i = 2; i <= n; ++i) fout << dist[i] << " ";
fout.close();
return 0;
}