#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int N=100005;
const long long INF=0x3f3f3f3f3f3f3f3f;
int n,m,s,u,v,w,l[N];
long long d[N];
struct node{
int v;
long long w;
bool operator <(const node&x)const{
return x.w<w;
}
};
vector<node>a[N];
priority_queue<node>p;
bool b[N];
void print(int x){
if(l[x]!=0)print(l[x]);
printf("%d ",x);
}
int main(){
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d%d",&u,&v,&w);
a[u].push_back({v,w});
a[v].push_back({u,w});
}
memset(d,0x3f,sizeof(d));
d[1]=0;
p.push({1,0});
while(!p.empty()){
int u=p.top().v;
p.pop();
if(b[u])continue;
b[u]=true;
for(node j:a[u]){
if(d[u]+j.w<d[j.v]){
d[j.v]=d[u]+j.w;
l[j.v]=u;
p.push({j.v,d[j.v]});
}
}
}
if(d[n]==INF)printf("-1");
else print(n);
return 0;
}