#include #include #include using std::vector; using std::queue; int distance(vector > &adj, int s, int t) { vector d(adj.size(), -1); queue q; q.push(s); d[s] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (int i = 0; i < adj[v].size(); i++) { int w = adj[v][i]; if (d[w] == -1) { q.push(w); d[w] = d[v] + 1; } } } return d[t]; } int main() { int n, m; std::cin >> n >> m; vector > adj(n, vector()); for (int i = 0; i < m; i++) { int x, y; std::cin >> x >> y; adj[x - 1].push_back(y - 1); adj[y - 1].push_back(x - 1); } int s, t; std::cin >> s >> t; s--, t--; std::cout << distance(adj, s, t) << '\n'; }