#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
using namespace std;
void BFS(const vector<set<long long>>& adj, vector<long long>& distances){
queue<long long> q;
q.push(0);
distances[0] = 0;
while(not q.empty()){
long long x = q.front();
q.pop();
//cout<<q.size()<<endl;
long long idx=0;
while(idx!=adj.size()-1){
long long setint;
if(adj[x].begin()==adj[x].end()){
setint = adj.size()-1;
}
else setint = *adj[x].begin();
while(idx!=setint){
if(distances[idx]<0){
distances[idx]=distances[x]+1;
q.push(idx);
}
idx += 1;
}
if(adj[x].begin()!=adj[x].end()){
set<long long>::const_iterator it = adj[x].begin();
//it = adj[x].begin();
adj[x].erase(it);
}
}
}
}
void Gpath(const vector<set<long long>>& adj, const vector<long long>& distances, long long ini, stack<long long>& path){
int x=ini;
path.push(x);
while (distances[x]!=0){
for(unsigned long i=0;i<adj.size(); i++){
//cout << "."<<endl;
if(adj[x].find(i) != adj[x].end())continue;
if(distances[i]!=-1 and distances[i]<distances[x]){
path.push(i);
x=i;
break;
}
}
}
}
int main(){
int n,m;
cin >> n >> m;
vector<set<int>> adj(n);
for(int i=0;i<n;i++)adj[i].insert(i); //[i]=false;
for(int i=0;i<m;i++){
int a,b;
cin >> a >> b;
adj[a-1].insert(b-1);
adj[b-1].insert(a-1);
}
vector<int> distances(n,-1);
BFS(adj, distances);
//cout << "hola" << endl;
if(distances[n-1]==-1){
cout<<0<<endl;
return 0;
}
stack<int> path;
Gpath(adj,distances,n-1,path);
//cout << "adeu" << endl;
cout<<path.size()<<endl;
while(not path.empty()){
cout<<path.top()+1;
if(path.top()!=n-1)cout<<" ";
path.pop();
}
cout <<endl;
}