output_description: Print the minimum cost required to turn the original sequence into b_{1},
  b_{2}, ..., b_{N}. If the task is impossible, output -1 instead.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      a_{1} a_{2} ... a_{N}
      b_{1} b_{2} ... b_{N}
attempt: #!usr/bin/env python3
  from collections import defaultdict, deque
  from heapq import heappush, heappop
  import sys
  import math
  import bisect
  import random
  
  
  def LI():
      return [int(x) for x in sys.stdin.readline().split()]
  
  
  def I():
      return int(sys.stdin.readline())
  
  
  def LS():
      return [list(x) for x in sys.stdin.readline().split()]
  
  
  def S():
      res = list(sys.stdin.readline())
      if res[-1] == "\n":
          return res[:-1]
      return res
  
  
  def IR(n):
      return [I() for i in range(n)]
  
  
  def LIR(n):
      return [LI() for i in range(n)]
  
  
  def SR(n):
      return [S() for i in range(n)]
  
  
  def LSR(n):
      return [LS() for i in range(n)]
  
  
  sys.setrecursionlimit(1000000)
  mod = 1000000007
  
  
  def solve():
      n = I()
      a = LI()
      b = LI()
      ans = 0
      l = []
      m = 51
      for num in range(1, m + 1)[::-1]:
          res = list(range(1, num)) + l
          d = [[0 if i == j else float("inf") for j in range(m)] for i in range(m)]
          for k in res:
              for i in range(m):
                  d[i][i % k] = 0
          for k in range(m):
              for i in range(m):
                  for j in range(m):
                      nd = d[i][k] + d[k][j]
                      if nd < d[i][j]:
                          d[i][j] = nd
  
          for i in range(n):
              if d[a[i]][b[i]] == float("inf"):
                  break
          else:
              continue
          if num == m:
              print(-1)
              return
          else:
              ans += 1 << num
              l.append(num)
      print(ans)
      return
  
  
  # Solve
  if __name__ == "__main__":
      solve()
  
problem_description: Statement
  
  Aoki is playing with a sequence of numbers a_{1}, a_{2}, ..., a_{N}. Every
  second, he performs the following operation :
  
    * Choose a positive integer k. For each element of the sequence v, Aoki may choose to replace v with its remainder when divided by k, or do nothing with v. The cost of this operation is 2^{k} (regardless of how many elements he changes).
  
  Aoki wants to turn the sequence into b_{1}, b_{2}, ..., b_{N} (the order of
  the elements is important). Determine if it is possible for Aoki to perform
  this task and if yes, find the minimum cost required.
samples: [{"input": "3\n    19 10 14\n    0 3 4", "output": "160\n    \n\nHere's a possible sequence of operations :\n\n  * Choose k = 7. Replace 19 with 5, 10 with 3 and do nothing to 14. The sequence is now 5, 3, 14.\n\n  * Choose k = 5. Replace 5 with 0, do nothing to 3 and replace 14 with 4. The sequence is now 0, 3, 4.\n\nThe total cost is 2^{7} + 2^{5} = 160.\n\n* * *"}, {"input": "3\n    19 15 14\n    0 0 0", "output": "2\n    \n\nAoki can just choose k = 1 and turn everything into 0. The cost is 2^{1} = 2.\n\n* * *"}, {"input": "2\n    8 13\n    5 13", "output": "-1\n    \n\nThe task is impossible because we can never turn 8 into 5 using the given\noperation.\n\n* * *"}, {"input": "4\n    2 0 1 8\n    2 0 1 8", "output": "0\n    \n\nAoki doesn't need to do anything here. The cost is 0.\n\n* * *"}, {"input": "1\n    50\n    13", "output": "137438953472\n    \n\nBeware of overflow issues."}]