Problem:
Score : 700 points

Problem 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.

Constraints

1 \leq N \leq 50

0 \leq a_{i}, b_{i} \leq 50

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N
a_{1} a_{2} ... a_{N}
b_{1} b_{2} ... b_{N}

Output

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.

Sample Input 1

3
19 10 14
0 3 4

Sample Output 1

160

Here's a possible sequence of operations :

Choose k = 7. Replace 19 with 5, 10 with 3 and do nothing to 14. The sequence is now 5, 3, 14.

Choose k = 5. Replace 5 with 0, do nothing to 3 and replace 14 with 4. The sequence is now 0, 3, 4.

The total cost is 2^{7} + 2^{5} = 160.

Sample Input 2

3
19 15 14
0 0 0

Sample Output 2

2

Aoki can just choose k = 1 and turn everything into 0. The cost is 2^{1} = 2.

Sample Input 3

2
8 13
5 13

Sample Output 3

-1

The task is impossible because we can never turn 8 into 5 using the given operation.

Sample Input 4

4
2 0 1 8
2 0 1 8

Sample Output 4

0

Aoki doesn't need to do anything here. The cost is 0.

Sample Input 5

1
50
13

Sample Output 5

137438953472

Beware of overflow issues.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class Main {
  public static int[] a,b;
  public static int n;
  public static int[][] necessary;
    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        // Scanner scan = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        n = Integer.parseInt(bf.readLine());
        StringTokenizer st = new StringTokenizer(bf.readLine());
        a = new int[n];
        b = new int[n];

        for(int i=0; i<n; i++) a[i] =  Integer.parseInt(st.nextToken());

        st = new StringTokenizer(bf.readLine());
        for(int i=0; i<n; i++) b[i] =  Integer.parseInt(st.nextToken());

        necessary = new int[51][51];
        for(int i=0; i<51; i++) for(int j=0; j<51; j++) necessary[i][j] = -1;
        for(int mod=1; mod<=50; mod++) {
          for(int i=0; i<51; i++) {
            for(int j=0; j<i; j++) {
              if(necessary[i][j] != -1) continue;
              int newi = i % mod;
              if(newi < j) continue;
              if((newi == j) || (necessary[newi][j] != -1)) necessary[i][j] = mod;
            }
          }
        }
        //System.out.println(necessary[19][0]);
        for(int i=0; i<n; i++) {
          if((a[i] != b[i]) && (necessary[a[i]][b[i]] == -1)) {
            out.println("-1");
            out.close(); System.exit(0);
          }
        }
        Set<Integer> mods = new HashSet<Integer>();
        while(true) {
          int max_nec = -1;
          int index = -1;
          for(int i=0; i<n; i++) {
            int val = a[i];
            while((val != b[i])) {
              int nec = necessary[val][b[i]];
              if(nec > max_nec) {
                max_nec = nec;
                index = i;
              }
              val %= nec;
            }
          }
          if(max_nec == -1) break;
          mods.add(max_nec);
        //  out.println(a[index] + " " + max_nec);
          a[index] %= max_nec;
          for(int i=0; i<n; i++) {
            boolean[] dp = new boolean[51];
            dp[b[i]] = true;
            for(int m : mods) {

              for(int k=b[i]+1; k<51; k++)
                dp[k] = dp[k] || dp[k%m];

            }
            if(dp[a[i]]) a[i] = b[i];
          }

        }
        long ans = 0;
        for(int i : mods) ans += 1L<<i;
        out.println(ans);

        out.close(); System.exit(0);
    }



}