lib LibC fun getchar_unlocked : Char end def read x = 0 loop do c = LibC.getchar_unlocked break unless 48 <= c <= 57 x &*= 10 x &+= c &- 48 end x end n = read s = read t = read a = Pointer(Int32).malloc(n &+ 2) n.times do |i| a[i] = read end a[n] = s a[n &+ 1] = t hash = Hash(Int32, Int32).new(n &+ 2) n.times do |i| hash[a[i]] = i end hash[s] = n hash[t] = n &+ 1 que = Pointer(Int32).malloc(n &+ 2) que[0] = n size = 1 dist = Pointer(Int32).malloc(n &+ 2) dist[n] = 1 while size != 0 i = que.value que += 1 size &-= 1 d = dist[i] ai = a[i] puts(d &- 2) + exit if i == n &+ 1 bit = 1 30.times do if (j = hash[ai ^ bit]?) && dist[j] == 0 dist[j] = d &+ 1 que[size] = j size &+= 1 end bit <<= 1 end end puts -1