#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
int n, N;
int x[301];
int cost[301];
int ans = 1000000000;
int m[301];
int DP[301][1<<9];
int dp(int cur, int mask)
{
if(cur == n+1)
{
if(mask == (1<<N)-1)
return 0;
return 1000000000;
}
int &ret = DP[cur][mask];
if(ret != -1) return ret;
ret = 1000000000;
ret = min(ret, dp(cur + 1, mask));
ret = min(ret, dp(cur + 1, mask | m[cur]) + cost[cur]);
return ret;
}
int MAIN()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> x[i];
}
for(int i = 1; i <= n; i++)
cin >> cost[i];
for(int i = 1; i <= n; i++)
{
vector <int> pFactors;
int t = x[i];
for(int j = 2; j*j <= t; j++)
if(t % j == 0)
{
pFactors.push_back(j);
while(t % j == 0)
t /= j;
}
if(t > 1)
pFactors.push_back(t);
N = pFactors.size();
for(int j = 1; j <= n; j++)
{
m[j] = 0;
for(int k = 0; k < N; k++)
if(x[j] % pFactors[k] != 0)
m[j] |= (1<<k);
}
memset(DP, 0xff, sizeof(DP));
ans = min(ans, cost[i] + dp(1, 0));
}
if(ans == 1000000000)
ans = -1;
cout << ans << endl;
return 0;
}
int main()
{
#ifdef LOCAL_TEST
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios :: sync_with_stdio(false);
cout << fixed << setprecision(16);
return MAIN();
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPGNvbXBsZXg+CiNpbmNsdWRlIDxjc3RyaW5nPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2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