#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 5;
int n, a[MAXN], c[MAXN];
map<int, int> mp;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    mp.clear();
    for (int i = 1; i <= n; i++) {
      for (map<int, int>::iterator iter = mp.begin(); iter != mp.end();
           iter++) {
        int temp = gcd(iter->first, a[i]);
        if (!mp[temp])
          mp[temp] = iter->second + c[i];
        else
          mp[temp] = min(mp[temp], iter->second + c[i]);
      }
      if (!mp[a[i]])
        mp[a[i]] = c[i];
      else
        mp[a[i]] = min(mp[a[i]], c[i]);
    }
    if (mp[1])
      printf("%d\n", mp[1]);
    else
      printf("-1\n");
  }
  return 0;
}
