#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + 1); // adjacency list
vector<bool> snake(n + 1, false); // stores snake paths
vector<int> parent(n + 1, -1); // parent array for path reconstruction
vector<bool> visited(n + 1, false); // visited array
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
queue<int> q;
q.push(1);
visited[1] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == n) break;
for (int v : adj[u]) {
if (!visited[v]) {
visited[v] = true;
parent[v] = u;
q.push(v);
}
}
}
if (!visited[n]) {
cout << 0 << endl;
} else {
vector<int> path;
for (int v = n; v != -1; v = parent[v]) {
path.push_back(v);
}
reverse(path.begin(), path.end());
cout << path.size() << endl;
for (int v : path) {
cout << v << " ";
}
cout << endl;
}
return 0;
}