#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,int> II;
const LL inf=2e17;
const int mx=1e5+5;
vector<vector<int> > g(mx),c(mx);
set<II> pq;
vector<int> path;
bitset<mx> visited;
int n,m;
int p[mx];
LL dist[mx];
void clr(){
for(int i=0; i<mx; ++i){
g[i].clear();
c[i].clear();
}
pq.clear();
path.clear();
visited.reset();
}
void init(){
for(int i=0; i<=n; ++i){
dist[i]=inf;
p[i]=-1;
}
dist[1]=0;
}
void dijkstra(){
init();
pq.insert({dist[1],1});
while(!pq.empty()){
int node=pq.begin()->second;
int cost=pq.begin()->first;
pq.erase(pq.begin());
if(node==n) break;
vector<int> v=g[node];
vector<int> w=c[node];
for(int i=0; i<v.size(); ++i){
if(dist[v[i]]>(LL)cost+w[i]){
II x(dist[v[i]],v[i]);
auto it=pq.find(x);
if(it!=pq.end()){
pq.erase(it);
}
dist[v[i]]=(LL)cost+w[i];
p[v[i]]=node;
pq.insert({dist[v[i]],v[i]});
}
}
}
int x=n;
while(true){
path.push_back(x);
x=p[x];
if(x==-1) break;
}
}
void dfs(int node){
visited[node]=1;
vector<int> v=g[node];
for(int i=0; i<v.size(); ++i){
if(!visited[v[i]]){
dfs(v[i]);
}
}
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(cin>>n>>m){
clr();
for(int i=0; i<m; ++i){
int u,v,w;
cin>>u>>v>>w;
g[u].push_back(v);
g[v].push_back(u);
c[u].push_back(w);
c[v].push_back(w);
}
dfs(1);
if(!visited[n]){
cout<<-1<<endl;
continue;
}
dijkstra();
for(int i=path.size()-1; i>=0; --i){
cout<<path[i]<<" ";
}
cout<<endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBsb25nIGxvbmcgTEw7CnR5cGVkZWYgcGFpcjxMTCxpbnQ+IElJOwpjb25zdCBMTCBpbmY9MmUxNzsKY29uc3QgaW50IG14PTFlNSs1Owp2ZWN0b3I8dmVjdG9yPGludD4gPiBnKG14KSxjKG14KTsKc2V0PElJPiBwcTsKdmVjdG9yPGludD4gcGF0aDsKYml0c2V0PG14PiB2aXNpdGVkOwppbnQgbixtOwppbnQgcFtteF07CkxMIGRpc3RbbXhdOwoKdm9pZCBjbHIoKXsKICAgIGZvcihpbnQgaT0wOyBpPG14OyArK2kpewogICAgICAgIGdbaV0uY2xlYXIoKTsKICAgICAgICBjW2ldLmNsZWFyKCk7CiAgICB9CiAgICBwcS5jbGVhcigpOwogICAgcGF0aC5jbGVhcigpOwogICAgdmlzaXRlZC5yZXNldCgpOwp9Cgp2b2lkIGluaXQoKXsKICAgIGZvcihpbnQgaT0wOyBpPD1uOyArK2kpewogICAgICAgIGRpc3RbaV09aW5mOwogICAgICAgIHBbaV09LTE7CiAgICB9CiAgICBkaXN0WzFdPTA7Cn0KCnZvaWQgZGlqa3N0cmEoKXsKICAgIGluaXQoKTsKICAgIHBxLmluc2VydCh7ZGlzdFsxXSwxfSk7CiAgICB3aGlsZSghcHEuZW1wdHkoKSl7CiAgICAgICAgaW50IG5vZGU9cHEuYmVnaW4oKS0+c2Vjb25kOwogICAgICAgIGludCBjb3N0PXBxLmJlZ2luKCktPmZpcnN0OwogICAgICAgIHBxLmVyYXNlKHBxLmJlZ2luKCkpOwoKICAgICAgICBpZihub2RlPT1uKSBicmVhazsKCiAgICAgICAgdmVjdG9yPGludD4gdj1nW25vZGVdOwogICAgICAgIHZlY3RvcjxpbnQ+IHc9Y1tub2RlXTsKCiAgICAgICAgZm9yKGludCBpPTA7IGk8di5zaXplKCk7ICsraSl7CiAgICAgICAgICAgIGlmKGRpc3RbdltpXV0+KExMKWNvc3Qrd1tpXSl7CiAgICAgICAgICAgICAgICBJSSB4KGRpc3RbdltpXV0sdltpXSk7CiAgICAgICAgICAgICAgICBhdXRvIGl0PXBxLmZpbmQoeCk7CiAgICAgICAgICAgICAgICBpZihpdCE9cHEuZW5kKCkpewogICAgICAgICAgICAgICAgICAgIHBxLmVyYXNlKGl0KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGRpc3RbdltpXV09KExMKWNvc3Qrd1tpXTsKICAgICAgICAgICAgICAgIHBbdltpXV09bm9kZTsKCiAgICAgICAgICAgICAgICBwcS5pbnNlcnQoe2Rpc3RbdltpXV0sdltpXX0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIGludCB4PW47CiAgICB3aGlsZSh0cnVlKXsKICAgICAgICBwYXRoLnB1c2hfYmFjayh4KTsKICAgICAgICB4PXBbeF07CiAgICAgICAgaWYoeD09LTEpIGJyZWFrOwogICAgfQp9Cgp2b2lkIGRmcyhpbnQgbm9kZSl7CiAgICB2aXNpdGVkW25vZGVdPTE7CgogICAgdmVjdG9yPGludD4gdj1nW25vZGVdOwogICAgZm9yKGludCBpPTA7IGk8di5zaXplKCk7ICsraSl7CiAgICAgICAgaWYoIXZpc2l0ZWRbdltpXV0pewogICAgICAgICAgICBkZnModltpXSk7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpCnsKLy8gICAgZnJlb3BlbigiaW4udHh0IiwiciIsc3RkaW4pOwovLyAgICBmcmVvcGVuKCJvdXQudHh0IiwidyIsc3Rkb3V0KTsKCiAgICB3aGlsZShjaW4+Pm4+Pm0pewogICAgICAgIGNscigpOwoKICAgICAgICBmb3IoaW50IGk9MDsgaTxtOyArK2kpewogICAgICAgICAgICBpbnQgdSx2LHc7CiAgICAgICAgICAgIGNpbj4+dT4+dj4+dzsKCiAgICAgICAgICAgIGdbdV0ucHVzaF9iYWNrKHYpOwogICAgICAgICAgICBnW3ZdLnB1c2hfYmFjayh1KTsKCiAgICAgICAgICAgIGNbdV0ucHVzaF9iYWNrKHcpOwogICAgICAgICAgICBjW3ZdLnB1c2hfYmFjayh3KTsKICAgICAgICB9CgogICAgICAgIGRmcygxKTsKICAgICAgICBpZighdmlzaXRlZFtuXSl7CiAgICAgICAgICAgIGNvdXQ8PC0xPDxlbmRsOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CgogICAgICAgIGRpamtzdHJhKCk7CgogICAgICAgIGZvcihpbnQgaT1wYXRoLnNpemUoKS0xOyBpPj0wOyAtLWkpewogICAgICAgICAgICBjb3V0PDxwYXRoW2ldPDwiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQ8PGVuZGw7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=