#include using namespace std; using ll=long long; int main(){ ll n,m; cin>>n>>m; vector> G(n); for(ll i=0;i>u>>v; u--; v--; G[u].push_back(v); } queue que; vector dist1(n,1e18); que.push(0); dist1[0]=0; while(que.size()){ ll q=que.front(); que.pop(); for(ll u:G[q]){ if(dist1[u]==1e18){ dist1[u]=dist1[q]+1; que.push(u); } } } vector dist2(n,1e18); que.push(n-2); dist2[n-2]=0; while(que.size()){ ll q=que.front(); que.pop(); for(ll u:G[q]){ if(dist2[u]==1e18){ dist2[u]=dist2[q]+1; que.push(u); } } } vector dist3(n,1e18); que.push(n-1); dist3[n-1]=0; while(que.size()){ ll q=que.front(); que.pop(); for(ll u:G[q]){ if(dist3[u]==1e18){ dist3[u]=dist3[q]+1; que.push(u); } } } ll ans=1e18; ans=min(ans,dist1[n-2]+dist2[n-1]+dist3[0]); ans=min(ans,dist1[n-1]+dist2[n-2]+dist3[0]); if(ans==1e18){ cout<<-1<