#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
#define MAX 50005
#define INF (1<<30)
queue < int > coada;
vector < pair < int, int > > v[MAX];
int cost[MAX], count[MAX];
int n, m, a, b, costul;
void bellmanford()
{
for(int i = 2; i<=n; i++)
cost[i] = INF;
coada.push(1);
while(!coada.empty())
{
int nod = coada.front();
for(int i = 0; i<v[nod].size(); i++)
{
int vecin = v[nod][i].first;
if(cost[vecin] > cost[nod] + v[nod][i].second){
cost[vecin] = cost[nod] + v[nod][i].second;
coada.push(vecin);
count[vecin]++;
if(count[vecin] > n)
{
fout << "Ciclu negativ!\n";
return;
}
}
}
coada.pop();
}
for(int i = 2; i<=n; i++)
fout << cost[i] << " ";
}
int main()
{
fin >> n >> m;
for(int i = 1; i<=m; i++)
{
fin >> a >> b >> costul;
v[a].push_back(make_pair(b,costul));
}
bellmanford();
fin.close();
fout.close();
return 0;
}