#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

// 1-indexed
template<typename T>
struct BIT{
    int n;
    vector<T> bit;
    BIT(int n_=0):n(n_),bit(n+1){}
    T sum(int i){
        T res=0;
        for(;i>0;i-=(i&-i))res+=bit[i];
        return res;
    }
    void add(int i,T a){
        if(i==0)return;
        for(;i<=n;i+=(i&-i)){bit[i]+=a;}
    }
    int lower_bound(T k){ // k<=sum(res)
        if(k<=0)return 0;
        int res=0,i=1;
        while((i<<1)<=n)i<<=1;
        for(;i;i>>=1){
            if(res+i<=n&&bit[res+i]<k)k-=bit[res+=i];
        }
        return res+1;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> a(n),b(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    map<int,vector<int>> mp;
    for(int i=n-1;i>=0;i--){
        mp[a[i]+i].push_back(i);
    }
    for(int i=0;i<n;i++){
        cin >> b[i];
        if(mp[b[i]+i].size()==0){
            printf("-1\n");
            return 0;
        }
        int id=mp[b[i]+i].back();
        mp[b[i]+i].pop_back();
        b[i]=id;
    }
    BIT<int> bit(n);
    ll res=0;
    for(int i=0;i<n;i++){
        res+=i-bit.sum(b[i]+1);
        bit.add(b[i]+1,1);
    }
    cout << res << endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

ll seg[1ll<<20];

void set_value(ll q, ll sl=0, ll sr=1ll<<19, ll pos=1) {
    if (q<sl || sr<=q) return;
    seg[pos]++;
    if (pos>=(1ll<<19)) return;
    ll sm = (sl+sr)/2;
    set_value(q,sl,sm,pos*2);
    set_value(q,sm,sr,pos*2+1);
    return;
}

ll get_cnt(ll ql, ll qr, ll sl=0, ll sr=1ll<<19, ll pos=1) {
    if (qr<=sl || sr<=ql) return 0;
    if (ql<=sl && sr<=qr) return seg[pos];
    ll sm = (sl+sr)/2;
    ll lsum = get_cnt(ql,qr,sl,sm,pos*2);
    ll rsum = get_cnt(ql,qr,sm,sr,pos*2+1);
    return lsum+rsum;
}
int main(){
    ll n; cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    REP(i,n) {cin >> a[i];a[i]+=i;}
    REP(i,n) {cin >> b[i];b[i]+=i;}
    vector<ll> as = a;
    vector<ll> bs = b;
    sort(as.begin(),as.end());
    sort(bs.begin(),bs.end());
    REP(i,n) {
        if (as[i]!=bs[i]) {
            cout << -1 << endl;
            return 0;
        }
    }
    vector<P> ap(n);
    vector<P> bp(n);
    REP(i,n) {ap[i]={a[i],i}; bp[i]={b[i],i};}
    sort(ap.begin(),ap.end());
    sort(bp.begin(),bp.end());
    vector<P> t(n);
    REP(i,n) {
        t[i]={ap[i].second+1ll,bp[i].second+1ll};
    }
    sort(t.begin(),t.end());
    // REP(i,n) cout << t[i].first << " " << t[i].second << endl;
    ll ans = 0;
    REP(i,n) {
        ll val = t[i].second;
        set_value(val);
        ans += get_cnt(val+1,1ll<<19);
    }
    cout << ans << endl;
    return 0;
}