from collections import deque def hamming_distance(a, b): tmp = bin(a ^ b) hd = 0 for i in range(len(tmp)): if hd == 0 and tmp[i] == '1': hd = 1 elif hd == 1 and tmp[i] == '1': hd = 2 break return hd N = int(input()) start, end = map(int, input().split()) stone = list(map(int, input().split())) #print(N, start, end, stone) all_stone = [start]+stone+[end] #print(hamming_distance(start, end), all_stone) g = [[] for _ in range(N+2)] # 隣接リスト for i in range(len(all_stone)-1): for j in range(i+1, len(all_stone)): dis = hamming_distance(all_stone[i], all_stone[j]) if dis == 1: g[i].append(j) g[j].append(i) # print(g) def bfs(u): queue = deque([u]) d = [None]*len(all_stone) # uからの距離の初期化 #print(queue, d) d[u] = 0 while queue: v = queue.popleft() for i in g[v]: if d[i] is None: d[i] = d[v]+1 queue.append(i) if i == len(all_stone)-1: break return d d = bfs(0) if d[len(all_stone)-1] is None: print(-1) else: print(d[len(all_stone)-1]-1)