#include #include #include using namespace std; using ll = long long; #include int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n,m; cin>>n>>m; vector> g(n); for(int i = 0;i>u>>v; u--;v--; g[u].push_back(v); } vector> ans(n,vector(4,1e9)); ans[0][0] = 0; using dat = pair>; priority_queue,greater> que; que.push(make_pair(0,make_pair(0,0))); while(que.size()){ int ni = que.top().second.first; int t = que.top().second.second; if(que.top().first>ans[ni][t]) { que.pop(); continue; } que.pop(); for(auto&to:g[ni]){ int need = ans[ni][t] + 1; int nt = t; if(to==n-2) nt |= 1; if(to==n-1) nt |= 2; if(ans[to][nt]<=need) continue; ans[to][nt] = need; que.push(make_pair(need,make_pair(to,nt))); } } if(ans[0][3]==1e9) ans[0][3] = -1; cout<