#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 61;

int64 po[51];
int N, A[50], B[50];

bool dfs(int64 bit, int cur_val, int to_val) {
  if(cur_val == to_val) return true;
  for(int i = cur_val; i >= 1; i--) {
    if((bit >> i) & 1 && dfs(bit, cur_val % i, to_val)) return true;
  }
  return false;
}

bool check(int64 bit) {
  for(int i = 0; i < N; i++) {
    if(!dfs(bit, A[i], B[i])) return false;
  }
  return true;
}

int main() {

  po[0] = 1;
  for(int i = 1; i < 51; i++) {
    po[i] = po[i - 1] * 2;
  }

  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for(int i = 0; i < N; i++) {
    cin >> B[i];
  }
  int64 ret = INF, bit = (1LL << 51) - 1;

  if(!check(bit)) {
    cout << -1 << endl;
    return (0);
  }

  for(int i = 50; i >= 0; i--) {
    int64 next_bit = bit ^(1LL << i);
    if(check(next_bit)) bit = next_bit;
  }

  int64 sum = 0;
  for(int i = 50; i >= 0; i--) {
    if((bit >> i) & 1) sum += po[i];
  }
  cout << sum << endl;
}
