#include <bits/stdc++.h>
/*#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <sstream>*/

#define ll long long
#define fop(i,m,n) for(int i=m; i<n; i++)
#define fastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()

using namespace std;

const long long MOD = 1e9+7;
const long long N = 1e9+5;
const long long Nlog = 17;
const long long Hash = 257;

const double PI = 2.0*acos(0.0);//<cmath>
//const double PI = 3.141592653;
const double E = 2.718281828;

struct Seg{
    int l, r, m;
    int val;
    Seg* ch[2];
    Seg(int inl, int inr) : l(inl), r(inr), m((l+r)>>1){
        if(r-l == 1) val = 0;
        else{
            ch[0] = new Seg(l, m);
            ch[1] = new Seg(m, r);
            pull();
        }
    }
    
    void pull(){
        val = ch[0]->val + ch[1]->val;
    }
    
    void set(int p, int v){
        if(r-l == 1) val = v;
        else{
            if(p < m) ch[0]->set(p, v);
            else ch[1]->set(p, v);
            pull();
        }
    }
    
    int query(int a, int b){
        if(r <= a || b <= l) return 0;
        if(a <= l && r <= b) return val;
        return ch[0]->query(a, b) + ch[1]->query(a, b);
    }
    
};

void solve(){
    
    int n;
    cin >> n;
    ll a[n], b[n];
    fop(i,0,n){
        cin >> a[i];
        a[i] += i;
    }
    fop(i,0,n){
        cin >> b[i];
        b[i] += i;
    }
    
    map<ll, deque<int> > m;
    fop(i,0,n){
        m[a[i]].pb(i);
    }
    int p[n];
    fop(i,0,n){
        if(m[b[i]].empty()){
            cout << "-1\n";
            return;
        }
        p[i] = m[b[i]].front();
        m[b[i]].pop_front();
    }
    
    //fop(i,0,n) cout << p[i] << ' ';
    //cout << '\n';
    
    ll ans = 0;
    Seg tree(0, n);
    fop(i,0,n){
        ans += tree.query(p[i], n);
        tree.set(p[i], 1);
    }
    cout << ans << '\n';
    
}


int main()
{
    fastIO;

    int t=1;
    //cin >> t;
    while(t--) solve();

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;

ll mod = 998244353;


//segment tree
//1-indexed all
class segtree {
public:
    ll n;
    vector<ll> A;
    segtree(ll k){
        n = 1;
        while(n<k){ n *= 2; }
        A=vector<ll>(2*n,0);
    }

    //a[i]にxを加算する
    void add(ll i,ll x){
        int index = n-1+i;
        A[index] += x;
        while(index>1){
            index /= 2;
            A[index] = A[2*index]+A[2*index+1];
        }
    }

    //a[i]をにする
    void replace(ll i,ll x){
        int index = n-1+i;
        A[index] = x;
        while(index>1){
            index /= 2;
            A[index] = A[2*index]+A[2*index+1];
        }
    }

    //a[i]+a[i+1]+…+a[j]を求める
    ll sum(ll i,ll j){
        return rangesum(i,j,1,1,n);
    }

    // a,b求める区間 k区間番号 c,d区間の始終点(k=1,c=1,d=nで入力する)
    ll rangesum(ll a,ll b,ll k,ll c,ll d){
        //単位元の設定
        ll el = 0;
        if(d<a||b<c){
            return el;
        }
        else if(a<=c&&d<=b){
            return A[k];
        }
        else{
            //2区間に分割して演算を行う
            ll p = rangesum(a,b,k*2,c,(c+d)/2)+rangesum(a,b,k*2+1,(c+d)/2+1,d);
            return p;
        }
    }

};

    

int main() {
    ll N; cin >> N;
    ll a[N],b[N];
    set<Pr> num; num.insert(make_pair(2e18,0LL));
    rep(i,N){
        cin >> a[i];
        num.insert(make_pair(a[i]+i,i));
    }
    vector<Pr> perm;
    rep(i,N){
        cin >> b[i];
        ll k = b[i]+i;
        auto itr = num.lower_bound(make_pair(k,-1));
        auto p = *itr;
        ll s = p.first;
        if(s!=k){
            cout << -1 << endl; return 0;
        }
        ll j = p.second;
        perm.push_back(make_pair(i+1,j+1));
        num.erase(itr);
    }
    ll ans = 0;
    //main関数内で
    segtree seq(N+1);
    //などと宣言し  seq.replace(i,x);
    //のように使う 1-indexed 注意!!!!!!
    sort(perm.begin(),perm.end());
    reverse(perm.begin(),perm.end());
    for(auto q:perm){
        ll j = q.second;
        //cout << j << endl;
        ans += seq.sum(1,j);
        seq.add(j,1);
    }
    cout << ans <<  endl;
}