#include using namespace std; int main(){ int n,m; cin>>n>>m; int a[m],b[m]; vector> g(n); for(int i = 0;i < m;i++){ cin>>a[i]>>b[i]; a[i]--; b[i]--; g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } queue q; q.push(0); vector cost(n,1000000); cost[0] = 0; while(!q.empty()){ int nn = q.front(); q.pop(); for(int x:g[nn]){ if(cost[x] == 1000000){ q.push(x); cost[x] = cost[nn]+1; } } } if(cost[n-1] == 1000000){ cout<<-1<