#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;

#define debug(x) cout << #x << '=' << x << '\n'
#define rep(i, b, e) for (int i = b; i <= e; i++)

const int N = 212345;

int n, a[N], b[N], c[N];
map<int, queue<int>> mp;

namespace Bit {
    int c[N], size;
    inline int lowbit(int x) {
        return x & (-x);
    }
    inline void add(int loc, int val) {
        for (; loc <= size; loc += lowbit(loc)) {
            c[loc] += val;
        }
    }
    inline int query(int loc) {
        int ret = 0;
        for (; loc; loc -= lowbit(loc)) {
            ret += c[loc];
        }
        return ret;
    }
}

int main(){
    cin >> n;
    rep (i, 1, n) {
        scanf("%d", a + i);
        a[i] += i;
        mp[a[i]].push(i);
    }
    rep (i, 1, n) {
        scanf("%d", b + i);
        b[i] += i;
        if (mp[b[i]].size() == 0) {
            puts("-1");
            return 0;
        }
        c[i] = mp[b[i]].front();
        mp[b[i]].pop();
    }
    //rep (i, 1, n) {
    //    debug(c[i]);
    //}
    Bit::size = n;
    long long ans = 0;
    for (int i = n; i > 0; i--) {
        ans += Bit::query(c[i] - 1);
        Bit::add(c[i], 1);
    }
    cout << ans;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int a[200010],b[200010];
queue<int>pos[200010];
map<int,int>id;
int tot,n;
int T[200010<<2];
int q(int rt,int l,int r,int pos){
	if(l==r)return T[rt];
	int mid=l+r>>1;
	if(pos<=mid)return T[rt]+q(rt<<1,l,mid,pos);
	else return T[rt]+q(rt<<1|1,mid+1,r,pos);
}
void ad(int rt,int l,int r,int L,int R){
	if(l==L&&r==R){
		T[rt]++;return;
	}
	int mid=l+r>>1;
	if(R<=mid)ad(rt<<1,l,mid,L,R);
	else if(L>mid)ad(rt<<1|1,mid+1,r,L,R);
	else ad(rt<<1,l,mid,L,mid),ad(rt<<1|1,mid+1,r,mid+1,R);
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[i]+=i;
		int t=id[a[i]];
		if(!t)id[a[i]]=t=++tot;
		pos[t].push(i);
	}
	int flag=1;
	for(int i=1;i<=n;i++){
		scanf("%d",&b[i]);
		b[i]+=i;
		int t=id[b[i]];
		if(t&&!pos[t].empty()){
			b[i]=pos[t].front();
			pos[t].pop();
		}
		else flag=0;
	}
	if(!flag)printf("-1");
	else{
		long long ans=0;
		for(int i=1;i<=n;i++)ans+=q(1,1,n,b[i]),ad(1,1,n,1,b[i]);
		printf("%lld",ans);
	}
}