#include <bits/stdc++.h>
using namespace std;

struct SegmentTree{
    int N;
    vector<int> tree;
    SegmentTree(vector<int>& v) {
        int sz = v.size();
        N = 1;
        while(N < sz) N *= 2;
        tree = vector<int>(2 * N, 0);
        for(int i = 0;i < sz;++i) tree[N + i] = v[i];
    }
    void update(int index) {
        index += N;
        tree[index]++;
        while(index > 0) {
            index /= 2;
            tree[index] = tree[2 * index] + tree[2 * index + 1];
        }
    }
    int sum(int left, int right) {
        left += N, right += N;
        int res = 0;
        while(right - left > 1) {
            if(left % 2 == 0) left /= 2;
            else {
                res += tree[left];
                left = left / 2 + 1;
            }
            if(right % 2 == 0) {
                res += tree[right];
                right = right / 2 - 1;
            }
            else right /= 2;
        }
        res += (left == right ? tree[left] : tree[left] + tree[right]);
        return res;
    }
};

int main() {
    int n; cin >> n;
    vector<int> a(n), b(n);
    for(auto& e : a) cin >> e;
    for(auto& e : b) cin >> e;
    for(int i = 0;i < n;++i) a[i] += i, b[i] += i;
    map<int, int> mpa, mpb;
    for(int i = 0;i < n;++i) mpa[a[i]]++, mpb[b[i]]++;
    for(auto p : mpa) {
        if(mpb[p.first] != p.second) {
            cout << -1 << '\n';
            return 0;
        }
    }
    map<int, queue<int>> mp;
    for(int i = 0;i < n;++i) mp[b[i]].push(i);
    vector<int> v1(n), v2(n);
    for(int i = 0;i < n;++i) {
        v1[i] = mp[a[i]].front();
        mp[a[i]].pop();
    }
    SegmentTree seg(v2);
    long long ans = 0;
    for(int i = 0; i < n;++i) {
        ans += seg.sum(0, v1[i]);
        seg.update(v1[i]);
    }
    ans = (long long)n * (n - 1) / 2 - ans;
    cout << ans << '\n';
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;

const int maxn=200000+10;
ll tree[maxn],n,a[maxn],b[maxn],c[maxn];

int lowbit(int i) {return i&-i;}
ll ask(int x)
{
    int res=0;
    for(int i=x;i;i-= lowbit(i)) res+=tree[i];
    return res;
}
void modify(int x,int val)
{
    for(int i=x;i<=n;i+=lowbit(i)) tree[i]+=val;
}

int main()
{
    cin>>n;
    vector <P> vec1,vec2;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        a[i]+=i;
        vec1.push_back(P(a[i],i));
    }
    for(int i=0;i<n;i++)
    {
        cin>>b[i];
        b[i]+=i;
        vec2.push_back(P(b[i],i));
    }
    sort(vec1.begin(),vec1.end());
    sort(vec2.begin(),vec2.end());
    for(int i=0;i<n;i++)
    {
        if(vec1[i].first!=vec2[i].first)
        {
            cout<<"-1\n";
            return 0;
        }
        c[vec1[i].second]=vec2[i].second;
    }
    ll ans=0;
    for(int i=n-1;i>=0;i--)
    {
        ans+=ask(c[i]+1);
        //cout<<ans<<endl;
        modify(c[i]+1,1);
    }
    cout<<ans<<endl;
    return 0;
}