#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 505;
char en = '\n';
ll inf = 1e16;
ll mod = 998244353;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

using namespace __gnu_pbds; //(or pb_ds)
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

char arr[N][N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;

  ll a[n + 5];
  ll b[n + 5];

  map<ll, vector<ll>> indices;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    indices[i + a[i]].push_back(i);
  }
  for (ll i = 1; i <= n; i++) {
    cin >> b[i];
  }

  for (auto &it : indices) {
    reverse(it.second.begin(), it.second.end());
  }
  pbds X;
  ll res = 0;

  for (ll i = 1; i <= n; i++) {
    ll req = b[i] + i;
    if (indices.find(req) == indices.end()) {
      cout << -1 << en;
      return 0;
    }
    vector<ll> &vect = indices[req];
    ll indx = vect.back();
    vect.pop_back();
    if (vect.empty()) {
      indices.erase(req);
    }

    // adjust for changes
    // find indices > indx
    ll change = (i - 1) - X.order_of_key(indx);

    X.insert(indx);
    res += indx + change - i;
  }
  cout << res << en;

  return 0;
}

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

using namespace std;

int n,i,x;
int a[200005];
long long ans;
vector<int> seg;
map<int, vector<int>> m;

void upd(int stt, int drt, int st, int dr, int p, int val)
{
    if(stt==st && drt==dr)
    {
        seg[p]+=val;
        return;
    }

    int mij=(st+dr)/2;

    if(drt<=mij)
        upd(stt, drt, st, mij, 2*p, val);
    else if(stt>mij)
        upd(stt, drt, mij+1, dr, 2*p+1, val);
    else
    {
        upd(stt, mij, st, mij, 2*p, val);
        upd(mij+1, drt, mij+1, dr, 2*p+1, val);
    }
}
int query(int i, int st, int dr, int p)
{
    if(st==dr)
        return seg[p];

    int mij=(st+dr)/2;

    if(i<=mij)
        return seg[p]+query(i, st, mij, 2*p);
    else
        return seg[p]+query(i, mij+1, dr, 2*p+1);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin>>n;

    seg.resize(4*n+4);

    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        a[i]+=i;
    }

    for(i=n;i>=1;i--)
    {
        upd(i, i, 1, n, 1, i);
        m[a[i]].push_back(i);
    }

    for(i=1;i<=n;i++)
    {
        cin>>x;
        x+=i;

        if(m[x].empty())
        {
            cout<<-1;
            return 0;
        }

        int p=m[x].back();
        m[x].pop_back();

        ans+=query(p, 1, n, 1)-i;

        if(p>1)
            upd(1, p-1, 1, n, 1, 1);
    }

    cout<<ans;
    return 0;
}
