#include using namespace std; using ll = long long; using P = pair; using T = tuple; // #include // using namespace atcoder; // using mint = modint1000000007; #define rep(i, n) for(ll i = 0; i < n; i++) int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); ll n, m; cin >> n >> m; vector> Graph(4*n); vector vis(4*n,-1); rep(i,m) { ll u, v; cin >> u >> v; u--; v--; Graph[u].emplace_back(v); Graph[n+u].emplace_back(n+v); Graph[2*n+u].emplace_back(2*n+v); Graph[3*n+u].emplace_back(3*n+v); } Graph[n-2].emplace_back(n+(n-2)); Graph[n-1].emplace_back(2*n+(n-1)); Graph[n+(n-1)].emplace_back(3*n+(n-1)); Graph[2*n+(n-2)].emplace_back(3*n+(n-2)); queue

todo; todo.push(P(0,0)); while( !todo.empty() ) { auto [idx,dis] = todo.front(); todo.pop(); if( vis[idx] != -1 ) continue; vis[idx] = dis; for(auto to:Graph[idx]) { if( vis[to] != -1 ) continue; todo.push(P(to,dis+1)); } } cout << (vis[3*n] == -1 ? -1 : vis[3*n]-2) << endl; return 0; }