local bxor = bit.bxor local f = {} do local mul = 1 while true do table.insert(f, mul) mul = mul * 2 if 1000000007 < mul then break end end end local n = io.read("*n") local a = {} a[1], a[2] = io.read("*n", "*n") for i = 3, n + 2 do a[i] = io.read("*n") end n = n + 2 local len = {} local ainv = {} for i = 1, n do len[i] = -1 ainv[a[i]] = i end len[1] = 0 local tasks = {1} local done = 0 while done < #tasks do done = done + 1 local src = tasks[done] for i = 1, #f do local d = bxor(a[src], f[i]) local dst = ainv[d] if dst and len[dst] < 0 then if dst == 2 then print(len[src]) os.exit() end len[dst] = len[src] + 1 table.insert(tasks, dst) end end end print(-1)