#include <bits/stdc++.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);
	const int MOD = 998244353;

	int N; cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
		A[i] += i;
	}
	vector<int> B(N);
	for (int i = 0; i < N; ++i) {
		cin >> B[i];
		B[i] += i;
	}
	for (int i = 0; i < N; ++i) cerr << A[i] << " \n"[i == N-1];
	for (int i = 0; i < N; ++i) cerr << B[i] << " \n"[i == N-1];
	map<int, vector<int>> Aloc;
	map<int, vector<int>> Bloc;
	for (int i = 0; i < N; ++i) {
		Aloc[A[i]].push_back(i);
		Bloc[B[i]].push_back(i);
	}
	vector<int> perm(N);
	for (int i = N - 1; i >= 0; --i) {
		if (Bloc[A[i]].empty()) {
			cout << "-1\n";
			return 0;
		}
		perm[i] = Bloc[A[i]].back();
		Bloc[A[i]].pop_back();
	}
	// count inversions
	vector<int> fenw(N);
	int64_t ans = 0;
	for (int v : perm) {
		for (int x = v; x < N; x |= (x + 1)) ans += fenw[x];
		for (int x = v; x >= 0; x = (x & (x + 1)) - 1) fenw[x] ++;
	}
	cout << ans << '\n';
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int,int>
#define f first
#define s second
set<pi> st;
const int nax=2e5+10;
int a[nax],b[nax];
struct node
{
    int n;
    int bit[nax+1];
    node(){}
    node(int _n):n(_n){
        memset(bit,0,sizeof(bit));
    }
    void add(int pos,int val)
    {
        if(pos<=0)
        return;
        while(pos<=n)
        {
            bit[pos]+=val;
            pos+=(pos&-pos);
        }
    }
    int qry(int pos)
    {
        int res=0;
        pos=min(pos,n);
        while(pos)
        {
            res+=bit[pos];
            pos-=(pos&-pos);
        }
        return res;
    }
};
int32_t main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
    for(int i=0;i<n;i++)
    cin>>b[i];
    node root(n);
    for(int i=0;i<n;i++)
    {
        st.insert({a[i]+i,i});
        root.add(i+1,1);
    }
    int res=0;
    for(int i=0;i<n;i++)
    {
        int cur=b[i]+i;
        set<pi>::iterator it=st.lower_bound({cur,-1});
        if(it!=st.end() and it->f==cur)
        {
            int j=it->s;
            res+=root.qry(j);
            st.erase(it);
            root.add(j+1,-1);
        }
        else
        {
            cout<<"-1";
            return 0;
        }
    }
    cout<<res;
    return 0;
}
