import queue def get_hd(a,b): return bin(a^b).count('1') def solve_slower(start, end, stones): checked_set = set() que = queue.Queue() que.put((start, 0)) while not que.empty(): current, distance = que.get() next_distance = distance + 1 if get_hd(current, end) == 1: return next_distance - 1 for candidate in stones: if candidate in checked_set: continue if get_hd(current, candidate) == 1: checked_set.add(candidate) que.put((candidate, next_distance)) return -1 if __name__ == '__main__': N = int(input()) start, end = list(map(int, input().split())) stones = list(map(int, input().split())) print(solve(start, end, stones))