#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const int MAX_N = 100101; struct Node { int v; int dist; Node(int v = -1, int dist = -1) { this->v = v; this->dist = dist; } bool operator>(const Node &n) const { return dist > n.dist; } }; vector E[MAX_N]; int main() { int N, M; cin >> N >> M; priority_queue , greater> pque; int a, b; for (int i = 0; i < M; ++i) { cin >> a >> b; E[a].push_back(b); E[b].push_back(a); } pque.push(Node(1, 0)); vector visited(MAX_N, false); while (not pque.empty()) { Node node = pque.top(); pque.pop(); if (visited[node.v]) continue; visited[node.v] = true; if (node.v == N) { cout << node.dist << endl; return 0; } for (int u : E[node.v]) { pque.push(Node(u, node.dist + 1)); } } cout << -1 << endl; return 0; }