#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int n;
map<int, int> dp;
struct node {
  int num, val;
} t[310];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> t[i].num;
  }
  for (i = 0; i < n; ++i) {
    cin >> t[i].val;
  }
  dp[0] = 0;
  for (i = 0; i < n; ++i) {
    int x = t[i].num;
    int y = t[i].val;
    map<int, int>::iterator it = dp.begin();
    while (it != dp.end()) {
      int xx = it->first;
      int yy = it->second;
      int g = gcd(xx, x);
      int v = yy + y;
      if (dp[g] && dp[g] < v) {
        ++it;
        continue;
      }
      dp[g] = v;
      ++it;
    }
  }
  if (!dp[1])
    puts("-1");
  else
    printf("%d\n", dp[1]);
  return 0;
}
