// ＵＴＦ−８

#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<double>;

/* #include<atcoder/all> */
/* using namespace atcoder; */

template<class T>bool chmax(T &a, const T &b) { return (a<b ? (a=b,1) : 0); }
template<class T>bool chmin(T &a, const T &b) { return (a>b ? (a=b,1) : 0); }

template <typename T>
struct SegmentTree {
    using F = function<T(T, T)>;

    const T e;
    const F f;
    size_t sz;
    vector<T> tree;

    SegmentTree(size_t n, const F &f, const T &e = 0) : f(f), e(e) {
        sz = 1;
        while(sz < n) sz <<= 1;
        tree.assign(2*sz, e);
    }

    void set(typename vector<T>::iterator begin, typename vector<T>::iterator end) {
        copy(begin, end, tree.begin() + sz);
        for(size_t k=sz-1; k>0; k--)
            tree[k] = f(tree[2*k+0], tree[2*k+1]);
    }

    void update(size_t k, const T &x) {
        k += sz;
        tree[k] = x;
        while(k >>= 1)
            tree[k] = f(tree[2*k+0], tree[2*k+1]);
    }

    T query(size_t a, size_t b) const {
        b = min(b, sz);
        a = max((size_t)0, a);
        T l = e, r = e;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) l = f(l, tree[a++]);
            if(b & 1) r = f(tree[--b], r);
        }
        return f(l, r);
    }

    T operator[](const size_t k) const {
        return tree[sz + k];
    }
};

int main(void) {
    constexpr ll MOD = 0 ? 1e9+7 : 998244353;
    const double PI = acos(-1);
    constexpr double eps = 1e-10;
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);


    if(1) {
        ll n;
        cin >> n;
        vector<ll> a(n), b(n), c(n);
        for(auto &e: a) cin >> e;
        for(auto &e: b) cin >> e;

        map<ll,vector<ll>> m;
        for(ll i=0; i<n; i++)
            m[a[i]+i].push_back(i);
        map<ll,ll> idx;

        for(ll i=0; i<n; i++) {
            ll x = b[i]+i;
            auto &v = m[x];
            ll y = idx[x]++;
            if(y == v.size()) {
                cout << -1 << endl;
                return 0;
            }
            c[i] = v[y];
        }

        ll r = 0;
        SegmentTree<ll> st(n, [&](ll a, ll b){return a + b;}, 0);
        for(ll i=0; i<n; i++) {
            r += i - st.query(0, c[i]);
            st.update(c[i], st[c[i]]+1);
        }

        cout << r << endl;
    }
    return 0;
}
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define ll long long
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
const int N=2e5+5;
struct node{int v,id;}a[N],b[N];
bool cmp(node x,node y){return x.v==y.v?x.id<y.id:x.v<y.v;}
int n,A[N],t[N];
void I(int pos){for(int i=pos;i<=n;i+=i&-i)++t[i];}
int Q(int pos){int res=0;for(int i=pos;i;i-=i&-i)res+=t[i];return res;}
ll ans;
int main(){
    n=read();
    rep(i,1,n)a[i]={read()+i,i};
    rep(i,1,n)b[i]={read()+i,i};
    sort(a+1,a+n+1,cmp);sort(b+1,b+n+1,cmp);
    rep(i,1,n){
        if(a[i].v!=b[i].v)return puts("-1"),0;
        A[a[i].id]=b[i].id;
    }
    for(int i=n;i;i--){
        I(A[i]);ans+=Q(A[i]-1);
    }
    printf("%lld\n",ans);
    return 0;
}