#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define inf 0x7fffffff
#define maxx 100009
priority_queue<pii,vector<pii>,greater<pii> >q;
vector<pii>G[maxx];
int d[maxx];
bool f[maxx];
int prevv[maxx];
int n,m;
void printpath(int src)
{
if(prevv[src]!=-1)
printpath(prevv[src]);
cout<<src<<" ";
}
void dijktras(int src)
{
for(int i=0;i<=n;i++)
{
d[i]=inf;
f[i]=false;
prevv[i]=-1;
}
d[src]=0;
q.push(pii(src,0));
while(!q.empty())
{
int s=q.top().first;
q.pop();
for(int i=0;i<G[s].size();i++)
{
int v=G[s][i].first;
int w=G[s][i].second;
if(!f[v] && d[s]+w<d[v])
{
d[v]=d[s]+w;
q.push(pii(v,d[v]));
prevv[v]=s;
}
}
f[s]=true;
}
}
int main() {
// your code goes here
memset(prevv,-1,sizeof prevv);
cin>>n>>m;
while(m--)
{
int a,b,w;
cin>>a>>b>>w;
G[a].pb(pii(b,w));
G[b].pb(pii(a,w));
}
dijktras(1);
if(d[n]==inf)
{
cout<<"-1"<<endl;
}
else
printpath(n);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgcGlpIHBhaXI8aW50LGludD4KI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBpbmYgMHg3ZmZmZmZmZgojZGVmaW5lIG1heHggMTAwMDA5CnByaW9yaXR5X3F1ZXVlPHBpaSx2ZWN0b3I8cGlpPixncmVhdGVyPHBpaT4gPnE7CnZlY3RvcjxwaWk+R1ttYXh4XTsKaW50IGRbbWF4eF07CmJvb2wgZlttYXh4XTsKaW50IHByZXZ2W21heHhdOwppbnQgbixtOwp2b2lkIHByaW50cGF0aChpbnQgc3JjKQp7CglpZihwcmV2dltzcmNdIT0tMSkKCXByaW50cGF0aChwcmV2dltzcmNdKTsKCWNvdXQ8PHNyYzw8IiAiOwp9CnZvaWQgZGlqa3RyYXMoa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