#include using namespace std; using ll = long long; vector bfs(vector> &E, int start){ int N = E.size(); int from, to; queue que; vector dist(N, 1e9); dist[start] = 0; que.push(start); while(!que.empty()){ from = que.front(); que.pop(); for (auto to : E[from]){ if (dist[to] != 1e9) continue; dist[to] = dist[from] + 1; que.push(to); } } return dist; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); int N, M, A, B; ll ans; cin >> N >> M; vector> E(N); for (int i=0; i> A >> B; A--; B--; E[A].push_back(B); } vector dist1 = bfs(E, 0), dist2 = bfs(E, N-2), dist3 = bfs(E, N-1); ans = min(dist1[N-2]+dist2[N-1]+dist3[0], dist1[N-1]+dist3[N-2]+dist2[0]); cout << (ans < 1e9 ? ans : -1) << endl; return 0; }