#include <bits/stdc++.h>
using namespace std;
int jump[300], cost[300];
int numCards;
map<pair<int, int>, long long> dpMem;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); };
long long dp(int cardNum, int gcdVal) {
  pair<int, int> queryPair = make_pair(cardNum, gcdVal);
  if (dpMem.count(queryPair)) return dpMem[queryPair];
  if (cardNum == numCards) {
    long long retVal;
    if (gcdVal == 1) {
      retVal = 0;
    } else {
      retVal = numeric_limits<int>::max();
    }
    return retVal;
  }
  long long costNotUsingCard = 0, costUsingCard = 0, minVal;
  int newGCD = gcd(gcdVal, jump[cardNum]);
  pair<int, int> notUsingCard = make_pair(cardNum + 1, gcdVal),
                 usingCard = make_pair(cardNum + 1, newGCD);
  costNotUsingCard =
      dpMem.count(notUsingCard) ? dpMem[notUsingCard] : dp(cardNum + 1, gcdVal);
  costUsingCard = dpMem.count(usingCard)
                      ? dpMem[usingCard] + cost[cardNum]
                      : dp(cardNum + 1, newGCD) + cost[cardNum];
  minVal = min(costNotUsingCard, costUsingCard);
  if (minVal == costNotUsingCard) {
    dpMem[make_pair(cardNum, gcdVal)] = costNotUsingCard;
  } else {
    dpMem[make_pair(cardNum, gcdVal)] = costUsingCard;
  }
  return minVal;
}
int main() {
  scanf("%d", &numCards);
  for (int i = 0; i < numCards; i++) {
    scanf("%d", &jump[i]);
  }
  for (int i = 0; i < numCards; i++) {
    scanf("%d", &cost[i]);
  }
  long long res = dp(0, 0);
  if (res < numeric_limits<int>::max()) {
    printf("%d\n", res);
  } else {
    printf("-1\n");
  }
  return 0;
}
