#include <bits/stdc++.h>
using Ld = long double;
using Vi = std::vector<int>;
using Pii = std::pair<int, int>;
using Ll = long long;
using namespace std;
int n;
Vi S, C;
int Solve() {
  int min_cost = -1;
  map<int, int> m1, m2;
  for (int i = 0; i < (int)(n); ++i) {
    auto it2 = m2.lower_bound(S[i]);
    if (it2 != m2.begin()) {
      auto pre = prev(it2);
      int newcost = pre->second + C[i];
      if (min_cost == -1)
        min_cost = newcost;
      else
        min_cost = min(min_cost, newcost);
    }
    auto it1 = m1.lower_bound(S[i]);
    if (it1 != m1.begin()) {
      auto pre = prev(it1);
      int newcost = pre->second + C[i];
      if (it2 == m2.begin() or prev(it2)->second > newcost) {
        auto [f, b] = m2.emplace(S[i], newcost);
        if (not b) f->second = min(f->second, newcost);
        auto nf = next(f), ff = nf;
        while (ff != m2.end() and ff->second >= f->second) ff++;
        if (nf != ff) m2.erase(nf, ff);
      }
      if (pre->second <= C[i]) continue;
    }
    auto [f, b] = m1.emplace(S[i], C[i]);
    if (not b) f->second = min(f->second, C[i]);
    auto nf = next(f), ff = nf;
    while (ff != m1.end() and ff->second >= C[i]) ff++;
    if (nf != ff) m1.erase(nf, ff);
  }
  return min_cost;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  cin >> n;
  S.resize(n), C.resize(n);
  for (int i = 0; i < (int)(n); ++i) cin >> S[i];
  for (int i = 0; i < (int)(n); ++i) cin >> C[i];
  cout << Solve() << endl;
}
