from heapq import* n=int(input()) a=[int(i)<<11 for i in input().split()] heapify(a) b=[*map(int,input().split())] m=2**11-1 r=1<<11 for i in range(n): h=a.copy() for j in range(-i,n-i):heappush(h,heappop(h)+(b[j]>>1<<11)+1) r=min(r,max(j&m for j in h)) print(r)