#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> P;
ll gcd(ll a, ll b){return b? gcd(b, a % b): a;}
ll quickpow(ll a, ll b){ll res = 1; while(b){if (b & 1) res = res * a; a = a * a; b >>= 1;} return res;}
// head

const int N = 2e5 + 5;
int a[N], b[N], n, num, ans, tmp[N];

map<pair<int, int>, int> mp;
map<int, int> cnt;

void merge(int l, int r) {
	int mid = (l + r) >> 1, i = l, j = mid + 1, now = l;
	while (i <= mid && j <= r) {
		if (a[i] < a[j]) tmp[now++] = a[i++];
		else {
			ans += mid - i + 1;
			tmp[now++] = a[j++];
		}
	}
	while (i <= mid) tmp[now++] = a[i++];
	while (j <= r) tmp[now++] = a[j++];
	for (int i = l; i <= r; i++) a[i] = tmp[i];
} 

void merge_sort(int l, int r) {
	if (l == r) return;
	int mid = (l + r) >> 1;
	merge_sort(l, mid);
	merge_sort(mid + 1, r);
	merge(l, r);
}

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] += i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> b[i];
		b[i] += i;
	}
	for (int i = 1; i <= n; i++) {
		mp[make_pair(++cnt[b[i]], b[i])] = ++num;
	}
	cnt.clear();
	for (int i = 1; i <= n; i++) {
		pair<int, int> now = make_pair(++cnt[a[i]], a[i]);
		if (!mp.count(now)) {
			cout << -1 << endl;
			return 0;
		}
		a[i] = mp[now];
		//cout<<a[i]<<" ";
	}//cout<<endl;
	merge_sort(1, n);
	cout << ans << endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(i=(a);i<=(b);++i)
#define per(i,a,b) for(i=(a);i>=(b);--i)
#define REP(i,a,b) for(i=(a);i< (b);++i)
#define PER(i,a,b) for(i=(a);i> (b);--i)
#define ERR(...) fprintf(stderr,__VA_ARGS__)
template<class T>inline void cmn(T &x,const T &a){if(a<x)x=a;}
template<class T>inline void cmx(T &x,const T &a){if(x<a)x=a;}
template<class T>inline bool bmn(T &x,const T &a){if(a<x){x=a;return true;}else return false;}
template<class T>inline bool bmx(T &x,const T &a){if(x<a){x=a;return true;}else return false;}
const ll p=998244353ll,pp=p*p;const int ps2=p-2ll;
inline void add(ll &x){if(x>= p)x-=p ;}
inline void sub(ll &x){if(x<0ll)x+=p ;}
inline void adp(ll &x){if(x>=pp)x-=pp;}
inline void sup(ll &x){if(x<0ll)x+=pp;}
inline ll expo(ll a,int b=ps2,ll m=p){
	ll s=1ll;
	while(b){
		if(b&1)s=s*a%p;
		b>>=1; a=a*a%p;
	}
	return s;
}

inline void ac();
//inline void in();int main(){in();int t;scanf("%d",&t);while(t--)ac();return 0;}
//int main(){int t;scanf("%d",&t);while(t--)ac();return 0;}
int main(){ac();return 0;}

const int MAXN=200005;
map<int,int>AM,BM;
map<pair<int,int>,int>M;
ll Ans;
int Tmp,S[MAXN];
inline void inc(const int &x){
	if(!x){puts("-1");exit(0);}
	Tmp=0;
	int i;
	for(i=x;i     ;i-=i&-i)Tmp+=S[i];
	for(i=x;i<MAXN;i+=i&-i)   ++S[i];
	Ans+=Tmp;
}
inline void ac(){
	int n,i,a,s=0;
	scanf("%d",&n);
	per(i,n,1){scanf("%d",&a);a-=i;    M[make_pair(a,++AM[a])]=i;}
	per(i,n,1){scanf("%d",&a);a-=i;inc(M[make_pair(a,++BM[a])]) ;}
	printf("%lld\n",Ans);
}
/*
0. int overflow, array bounds
1. special cases (n=1? n=0?)
2. do smth instead of nothing and stay organized
3. WRITE STUFF DOWN
4. DON'T GET STUCK ON ONE APPROACH
5. STAY CALM AND DON'T GET EXPLODED
*/
