#include using namespace std; int main(){ int N, M; cin >> N >> M; vector> G(N); for (int i = 0; i < M; i++){ int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); } long long ans = 1e18; vector> D; for (int i : {0, N - 2, N - 1}){ queue q; vector d(N, 1e18); q.push(i); d[i] = 0; while (!q.empty()){ int v = q.front(); q.pop(); for (int i : G[v]){ if (d[i] == 1e18){ d[i] = d[v] + 1; q.push(i); } } } D.push_back(d); } ans = min(ans, D[0][N - 2] + D[1][N - 1] + D[2][0]); ans = min(ans, D[0][N - 1] + D[2][N - 2] + D[1][0]); if (ans != 1e18){ cout << ans << endl; } else { cout << -1 << endl; } }