#include template bool chmin(T &a, const T &x) { return x < a ? a = x, true : false; } int main() { int N, M; std::cin >> N >> M; std::vector> G(4*N + 1); while (M--) { int u, v; std::cin >> u >> v; if (v < N-1) { for (int s = 0; s < (1 << 2); ++s) G[s*N + u].push_back(s*N + v); } if (v == N-1) { G[0b00*N + u].push_back(0b01*N + v); G[0b10*N + u].push_back(0b11*N + v); } if (v == N) { G[0b00*N + u].push_back(0b10*N + v); G[0b01*N + u].push_back(0b11*N + v); } } constexpr int inf = 1000000000; std::vector D(4*N + 1, inf); std::queue bfs; D[1] = 0; bfs.push(1); while (!bfs.empty()) { const int u = bfs.front(); bfs.pop(); for (const int &v : G[u]) if (D[v] == inf) D[v] = D[u] + 1, bfs.push(v); } if (D[0b11*N + 1] < inf) std::cout << D[0b11*N + 1] << '\n'; else std::cout << "-1" << '\n'; }