#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const int MAX_N = 50010; struct Node { int v; int dist; Node(int v = -1, int dist = -1) { this->v = v; this->dist = dist; } }; int main() { int N; cin >> N; int start, end; cin >> start >> end; bool exists[MAX_N]; memset(exists, false, sizeof(exists)); exists[start] = true; exists[end] = true; for (int i = 0; i < N; ++i) { int s; cin >> s; exists[s] = true; } queue que; que.push(Node(start, 0)); bool visited[MAX_N]; memset(visited, false, sizeof(visited)); while (not que.empty()) { Node node = que.front(); que.pop(); if (visited[node.v]) continue; visited[node.v] = true; if (node.v == end) { cout << node.dist - 1 << endl; return 0; } for (int i = 0; i < 16; ++i) { int v1 = node.v ^ (1 << i); if (exists[v1]) { que.push(Node(v1, node.dist + 1)); } } } cout << -1 << endl; return 0; }