#include <bits/stdc++.h>
using namespace std;
int n;
map<int, int> dp;
pair<int, int> a[307];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i].first);
    for (int i = 0; i < n; i++) scanf("%d", &a[i].second);
    dp.clear();
    map<int, int>::iterator it;
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
      int x = a[i].first;
      int c = a[i].second;
      for (it = dp.begin(); it != dp.end(); it++) {
        int y = it->first;
        int d = gcd(x, y);
        int temp = it->second + c;
        if (dp[d] && dp[d] < temp) continue;
        dp[d] = temp;
      }
    }
    if (!dp[1])
      puts("-1");
    else
      printf("%d\n", dp[1]);
  }
}
