import * as fs from "fs" const bitCount = (n: number): number => { n = n - ((n >>> 1) & 0x55555555) n = (n & 0x33333333) + ((n >>> 2) & 0x33333333) n = (n + (n >>> 4)) & 0x0f0f0f0f n = n + (n >>> 8) n = n + (n >>> 16) return n & 0x3f } const calcDist = (a: number, b: number): number => { return bitCount(a ^ b) } const bfs = (n: number, arr: number[]): number => { const que: number[] = [0] const dist = Array(n).fill(-1) dist[0] = 0 while (que.length !== 0) { const v = que.shift() for (let u = 0; u < n; u++) { if (dist[u] !== -1) continue if (calcDist(arr[v], arr[u]) !== 1) continue dist[u] = dist[v] + 1 que.push(u) } } return dist[n - 1] } const main = (args: string): void => { const input = args.trim().split("\n"); const N = +input.shift() const [start, end] = input.shift().split(" ").map(x => Number(x)) let stones = [start] stones = stones.concat(input.shift().split(" ").map(x => Number(x))) stones.push(end) const ans: number = bfs(N + 2, stones); console.log(ans === -1 ? ans : ans - 1) } main(fs.readFileSync('/dev/stdin', 'utf8'));