#include using namespace std; typedef pair P; int hammingDistance(int a, int b) { int x = a ^ b; int res = 0; while (x > 0) { res += x & 1; x >>= 1; } return res; } int main() { int n, start, end; cin >> n >> start >> end; vector stones(n); vector visited(n); for (int i=0; i> stones[i]; visited[i] = false; } map> weights; for (int i=0; i que; que.push(P(0, start)); while (!que.empty()) { P p = que.front(); que.pop(); if (hammingDistance(p.second, end) == 1) { cout << p.first << endl; return 0; } int weight = hammingDistance(0, p.second); // weight-1 for (int i: weights[weight-1]) { if (visited[i]) { continue; } if (hammingDistance(p.second, stones[i]) == 1) { visited[i] = true; que.push(P(p.first+1, stones[i])); } } // weight+1 for (int i: weights[weight+1]) { if (visited[i]) { continue; } if (hammingDistance(p.second, stones[i]) == 1) { visited[i] = true; que.push(P(p.first+1, stones[i])); } } } cout << "-1" << endl; return 0; }