#include <bits/stdc++.h>   using namespace std;   void Genawy() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);   }   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int n, cost[305];   int length[305];   map<pair<int, int>, int> dp;   int solve(int ind, int gc) {     if (ind == n) return gc == 1 ? 0 : 1e9;     if (dp.find({ind, gc}) != dp.end()) return dp[{ind, gc}];     dp[{ind, gc}] = solve(ind + 1, gc);     dp[{ind, gc}] =         min(dp[{ind, gc}], solve(ind + 1, gcd(gc, length[ind])) + cost[ind]);     return dp[{ind, gc}];   }   int main() {     Genawy();     cin >> n;     for (int i = 0; i < n; i++) cin >> length[i];     for (int i = 0; i < n; i++) cin >> cost[i];     int ans = solve(0, 0);     if (ans >= 1e9) ans = -1;     cout << ans << endl;     return 0;   }   