#include <bits/stdc++.h>
#define int long long
#define mod 998244353

using namespace std;

const int O = 3e5 + 5;

int tree[O * 4], lazy[O * 4];

void Push(int id, int l, int r){
    int &x = lazy[id];
    int mid = (l + r) / 2;
    if (x == 0) return;
    tree[id << 1] += x;
    lazy[id << 1] += x;
    tree[id << 1 | 1] += x;
    lazy[id << 1 | 1] += x;
    x = 0;
}

void Update(int id, int l, int r, int u, int v, int val){
    if (l > v || r < u) return;
    Push(id, l, r);
    if (u <= l && r <= v){
        tree[id] += val;
        lazy[id] += val;
        return;
    }
    int mid = (l + r) / 2;
    Update(id << 1, l, mid, u, v, val);
    Update(id << 1 | 1, mid + 1, r, u, v, val);
    tree[id] = tree[id << 1] + tree[id << 1 | 1];
}

int Get(int id, int l, int r, int u, int v){
    Push(id, l, r);
    if (l > v || r < u) return 0;
    if (u <= l && r <= v) return tree[id];
    int mid = (l + r) / 2;
    return Get(id << 1, l, mid, u, v) + Get(id << 1 | 1, mid + 1, r, u, v);
}

main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int n; cin >> n;
    vector <int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; ++ i) cin >> a[i], a[i] += i;
    for (int i = 1; i <= n; ++ i) cin >> b[i], b[i] += i;

    vector <int> val;
    for (int i = 1; i <= n; ++ i) val.push_back(b[i]);
    sort(val.begin(), val.end());
    val.resize(distance(val.begin(), (unique(val.begin(), val.end()))));

    vector <vector <int>> g; g.resize((int)val.size() + 1);
    for (int i = 1; i <= n; ++ i){
        int id = upper_bound(val.begin(), val.end(), a[i]) - val.begin() - 1;
        if (val[id] != a[i]) return cout << -1, 0;
        g[id].push_back(i);
    }

    int h = val.size();
    for (int i = 0; i < h; ++ i)
        reverse(g[i].begin(), g[i].end());
    int ans = 0;
    for (int i = 1; i <= n; ++ i){
        int id = upper_bound(val.begin(), val.end(), b[i]) - val.begin() - 1;
        int x = g[id].back();
        ans += (x + Get(1, 1, n, x, x) - i);
        Update(1, 1, n, 1, x, 1);
        g[id].pop_back();
    }
    cout << ans;
}

#include <bits/stdc++.h>
#define sz(c) int(c.size())
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
using namespace std;
using ll = long long;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifdef LOCAL
#include <local/debug.h>
#else
#define debug(...) (void)0
#endif

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, 0, N) cin >> A[i];
  vector<int> B(N);
  rep(i, 0, N) cin >> B[i];

  set<pair<int, int>> s;
  ordered_set<int> ord;
  rep(i, 0, N) {
    s.insert({A[i] + i, i + 1});
    ord.insert(i + 1);
  }

  ll res = 0;
  rep(i, 0, N) {
    int val = B[i] + i;
    auto it = s.lower_bound({val, 0});
    if (it == s.end() || it->first != val) {
      if (it != s.end())
        assert(it->first > val);
      res = -1;
      break;
    }

    int pos = it->second;
    ll order = ord.order_of_key(pos);
    res += order;
    debug(val, pos, order);
    s.erase(it);
    ord.erase(pos);
  }

  cout << res << '\n';
}
