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

#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y) 
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

typedef pair <int,int> pii; 

const int maxn=2e5+5;

set <pii> s;
int n;
long long c[maxn];
set <pii> :: iterator it;

int lowbit(int x) {
	return x&-x;
}

void add(int x,int k) {
	while(x<=n) c[x]+=k,x+=lowbit(x);
} 

long long ask(int x) {
	long long r=0;
	while(x) r+=c[x],x-=lowbit(x);
	return r;
}

int main() {
	n=read(9); long long ans=0;
	rep(i,1,n) s.insert(make_pair(read(9)+i,i));
	rep(i,1,n) {
		int x=read(9); it=s.lower_bound(make_pair(x+i,0));
		if(it==s.end() || (it->first)!=x+i) return puts("-1"),0;
		long long tmp=ask(it->second);
		add(1,1),add(it->second,-1);
		ans+=tmp+(it->second)-i;
		if(tmp+(it->second)-i<0) puts("UN");
		s.erase(it);
	}
	print(ans,'\n');
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int bit[300010]; 
int N; 
void add(int a, int b){
	int x = a; 
	while(x <= N){
		bit[x] += b; x += x & -x; 
	}
}
int sum(int a){
	int res = 0; 
	int x = a; 
	while(x > 0){
		res += bit[x]; x -= x & -x;
	}
	return res; 
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> N; 
	int A[200010]; int B[200010]; 
	int C[200010]; int D[200010]; 
	set<int> st; 
	map<int, int> mp; 
	for(int i = 0; i < N; i++){
		cin >> A[i]; A[i] += i; C[i] = A[i]; 
		st.insert(A[i]); 
	}
	for(int i = 0; i < N; i++){
		cin >> B[i]; B[i] += i; D[i] = B[i]; 
	}
	sort(C, C+N); sort(D, D+N); 
	for(int i = 0; i < N; i++){
		if(C[i] != D[i]){
			cout << -1 << "\n"; return 0; 
		}
	}
	long long ans = 0; int idx = 1; 
	for(auto it = st.begin(); it != st.end(); it++){
		mp[*it] = idx; idx ++; 
	}
	for(int i = 0; i < N; i++) A[i] = mp[A[i]]; 
	for(int i = 0; i < N; i++) B[i] = mp[B[i]]; 
	vector<int> E[200010]; 
	for(int i = 0; i < N; i++){
		E[B[i]].push_back(i); 
	}
	int itr[200010] = {}; 
	int F[200010] = {}; int G[200010] = {}; 
	for(int i = 0; i < N; i++){
		F[i] = E[A[i]].at(itr[A[i]]); itr[A[i]] ++; 
		G[F[i]] = i; 
	}
	// cout << G[0] << G[1] << G[2] << endl; 
	for(int i = 0; i < N; i++){
		ans += (long long) sum(G[i] + 1);
		add(G[i] + 1, 1); 
	}
	cout << (long long) N * (N - 1) / 2 - ans << "\n"; 
}