#include<iostream>
#include<vector>
#include<utility>
using namespace std;
int d[501] = {0,};
int check[501] = {0,};
vector<vector<pair<int, int> > > vec;
int main(){
vec.resize(501);
int n,m;
cin>>n>>m;
for(int i=0; i<m; i++){
int tmp1, tmp2, tmp3;
scanf("%d %d %d", &tmp1, &tmp2, &tmp3);
vec[tmp1].push_back(make_pair(tmp2,tmp3));
}
d[1] = 0;
check[1] = 1;
bool update = true;
for(int i = 0; i<=n && update; i++){
if(i == n){
cout<<-1 <<endl;
return 0;
}
update = false;
for(int j = 1; j<=n; j++){
if(!check[j]){
continue;
}
for(auto it = vec[j].begin(); it != vec[j].end(); it++){
if( !check[it->first] || d[it->first] > d[j] + it->second){
update = true;
check[it->first] = 1;
d[it->first] = d[j] + it->second;
}
}
}
}
for(int i = 2; i<=n; i++){
if(!check[i]){
cout<<-1<<endl;
}
else{
cout<<d[i]<<endl;
}
}
}