#include #define rep(i,n) for(int i=0;i<(n);++i) using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; vector> g(n); rep(i,m){ int a,b; cin >> a >> b; a--;b--; g[a].push_back(b); } auto bfs = [&](int s, int t) -> int { vector dist(n,1e9); dist[s] = 0; queue q; q.push(s); while(!q.empty()){ int p = q.front(); q.pop(); for(int x:g[p]){ if(dist[x]!=1e9) continue; dist[x] = dist[p]+1; q.push(x); } } return dist[t]; }; int ans = min(bfs(0,n-2)+bfs(n-2,n-1)+bfs(n-1,0),bfs(0,n-1)+bfs(n-1,n-2)+bfs(n-2,0)); if(ans>=1e9) cout << -1 << endl; else cout << ans << endl; }