n = int(input()) start,end = map(int,input().split()) stone = list(map(int,input().split())) stoneMax = max(stone) maxLen = int(len(bin(stoneMax)[2:])) setStone= set(stone) avoidStone = set() from collections import deque def bfs(u): queue = deque([u,0]) while queue: v = queue.popleft() dis = queue.popleft() h = serchHam(v) if end in h: print(dis) exit() for i in h: queue.append(i) queue.append(dis+1) print(-1) exit() def serchHam(a): h=[] abin = bin(a+2**maxLen)[2:] for i in range(1,maxLen+1): if abin[i] == '0': hh = 2**(maxLen-i)+a else: hh = a-2**(maxLen-i) if hh == end: h.append(hh) return h if hh in setStone: if hh in avoidStone: continue else: h.append(hh) avoidStone.add(hh) return h bfs(start)