#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
const string fn("bellmanford");
ifstream in(fn + ".in");
ofstream out(fn + ".out");
#define cin in
#define cout out
const int MAX = 50000;
const int INF = 1e9;
int n, m;
vector<pii> g[MAX + 5]; /// g[x] = {y, c} pentru arcul x -> y de cost c
int cost[MAX + 5]; /// cost[i] = drumul minim de la 1 la i
int cnt[MAX + 5]; /// cnt[i] = de cate ori a fost pus nodul i in coada
bool inq[MAX + 5]; /// inq[i] = true daca i este acum in coada
void read()
{
cin >> n >> m;
int x, y, c;
for (int i = 0; i < m; i++)
{
cin >> x >> y >> c;
g[x].push_back(make_pair(y, c)); /// graf orientat
}
}
void init()
{
for (int i = 1; i <= n; i++)
cost[i] = INF;
}
/// returneaza false daca exista circuit negativ
bool bellmanFord(int src)
{
queue<int> q;
cost[src] = 0;
q.push(src);
inq[src] = true;
cnt[src] = 1;
while (!q.empty())
{
int node = q.front();
q.pop();
inq[node] = false;
for (auto x : g[node])
{
if (cost[node] + x.second < cost[x.first])
{
cost[x.first] = cost[node] + x.second;
if (!inq[x.first])
{
if (++cnt[x.first] >= n) /// nod pus in coada de n ori => circuit negativ
return false;
q.push(x.first);
inq[x.first] = true;
}
}
}
}
return true;
}
void write()
{
for (int i = 2; i <= n; i++,cout<<' ')
{
if (cost[i] == INF)
cout << -1;
else
cout << cost[i];
}
cout << '\n';
}
int main()
{
read();
init();
if (!bellmanFord(1))
cout << "Ciclu negativ!\n";
else
write();
return 0;
}