#include <bits/stdc++.h>
using namespace std;

int N;
#define MAX_N 50
#define MAX_V 50
int D[MAX_V + 1][MAX_V + 1];
bool A[MAX_N][MAX_V + 1];
int B[MAX_N];
long long R;

int main() {
  cin >> N;
  for(int v = 0; v <= MAX_V; ++v) for(int w = 0; w <= MAX_V; ++w) {
    D[v][w] = INT_MAX;
    for(int k = MAX_V; k >= 1; --k) {
      if(v % k == w) D[v][w] = k;
    }
    if(v == w) D[v][w] = 0;
  }
  for(int u = 0; u <= MAX_V; ++u) {
    for(int v = 0; v <= MAX_V; ++v) for(int w = 0; w <= MAX_V; ++w) {
      D[v][w] = min(D[v][w], max(D[v][u], D[u][w]));
    }
  }
  for(int i = 0; i < N; ++i) { int a; cin >> a; A[i][a] = true; }
  for(int i = 0; i < N; ++i) cin >> B[i];
  while(true) {
    int k = 0;
    for(int i = 0; i < N; ++i) {
      int c = INT_MAX;
      for(int v = 0; v <= MAX_V; ++v) {
        if(A[i][v]) c = min(c, D[v][B[i]]);
      }
      k = max(k, c);
    }
    if(k == INT_MAX) { cout << -1 << endl; return 0; }
    if(k == 0) break;
    R += 1LL << k;
    for(int i = 0; i < N; ++i) {
      for(int v = 0; v <= MAX_V; ++v) {
        if(A[i][v]) A[i][v % k] = true;
      }
    }
  }
  cout << R << endl;
  return 0;
}
