#include <bits/stdc++.h>
using namespace std;
const int N = 3000, INF = 4e8;
int ft[N];
void update(int i, int n, int x) {
  while (i < n) {
    ft[i] = min(ft[i], x);
    i |= i + 1;
  }
}
int query(int i) {
  int x = INF;
  while (i >= 0) {
    x = min(x, ft[i]);
    i &= i + 1, i--;
  }
  return x;
}
int ss[N], cc[3][N], ii[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> ss[i];
  for (int i = 0; i < n; i++) cin >> cc[0][i];
  for (int i = 0; i < n; i++) ii[i] = i;
  sort(ii, ii + n,
       [](int i, int j) { return ss[i] < ss[j] || ss[i] == ss[j] && i > j; });
  for (int r = 0; r < 2; r++) {
    fill(ft, ft + n, INF);
    for (int h = 0; h < n; h++) {
      int i = ii[h];
      cc[r + 1][i] = query(i) + cc[0][i];
      update(i, n, cc[r][i]);
    }
  }
  int ans = INF;
  for (int i = 0; i < n; i++) ans = min(ans, cc[2][i]);
  cout << (ans != INF ? ans : -1) << '\n';
  return 0;
}
