from collections import deque #ハミング距離 def hamming(a,b): return bin(a^b).count('1') n=int(input()) start,end=map(int,input().split()) stone=list(map(int,input().split())) x=[start,end]+stone #BFS dist=[False]*(n+2) que=deque([0]) dist[0]=0 while que: v=que.popleft() for u in range(n+2): if hamming(x[v],x[u])==1 and dist[u]==False: dist[u]=dist[v]+1 que.append(u) res=dist[1] if not res: print(-1) else: print(res-1)