#include <fstream>
#include <queue>
using namespace std;
ifstream cin ("bellmanford.in");
ofstream cout ("bellmanford.out");
int d[50005];
int cnt[50005];
vector< pair<int,int> > g[50005];
int n,m;
struct chestie
{
int d;
int vf;
};
class Comparare
{
public:
bool operator() (chestie a,chestie b)
{
return a.d>=b.d;
}
};
priority_queue<chestie,vector<chestie>,Comparare> pq;
void bellman(int start)
{
for(int i=1;i<=n;i++)
d[i] = 2e9;
d[start] = 0;
chestie aux;
aux.d = 0;
aux.vf = start;
pq.push(aux);
while(!pq.empty())
{
aux = pq.top();
pq.pop();
if(d[aux.vf]!=aux.d)
continue;
for(auto muchie:g[aux.vf])
{
if(d[muchie.first] > aux.d + muchie.second)
{
cnt[muchie.first]++;
if(cnt[muchie.first]==n+1)
{
cout << "Ciclu negativ!";
return;
}
d[muchie.first] = aux.d + muchie.second;
chestie noua;
noua.vf = muchie.first;
noua.d = d[muchie.first];
pq.push(noua);
}
}
}
for(int i=2;i<=n;i++)
cout << d[i] << ' ';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=1;i<=m;i++)
{
int a,b,c;
cin >> a >> b >> c;
g[a].push_back({b,c});
}
bellman(1);
return 0;
}