#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 200010
using namespace std;
ll n,a[N],b[N],l,r,ans=0,bit[N],nxt[N],plc[N],nm[N];
bool vis[N];
vector<pair<ll,ll> > pa,pb,rd;
void upd(ll x,ll v)
{
	x++;
	while(x<N)
	{
		bit[x]+=v;
		x+=(x&(-x));
	}
	return;
}
void update(ll x)
{
	upd(0,1);
	upd(x,-1);
	return;
}
ll query(ll x)
{
	x++;
	ll ret=0;
	while(x>0)
	{
		ret+=bit[x];
		x-=(x&(-x));
	}
	return ret;
}
int main(){
	ll i;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&a[i]);
		a[i]+=i;
	}
	for(i=0;i<n;i++)
	{
		scanf("%lld",&b[i]);
		b[i]+=i;
	}
	for(i=0;i<n;i++)
	{
		pa.push_back(make_pair(a[i],i));
		pb.push_back(make_pair(b[i],i));
	}
	sort(pa.begin(),pa.end());
	sort(pb.begin(),pb.end());
	for(i=0;i<pa.size();i++)
	{
		if(pa[i].F!=pb[i].F)
		{
			puts("-1");
			return 0;
		}
		nm[pa[i].S]=pa[i].S;
		plc[pa[i].S]=pa[i].S;
		rd.push_back(make_pair(pb[i].S,pa[i].S));
	}
	sort(rd.begin(),rd.end());
	for(i=0;i<rd.size();i++)
	{
		ans+=abs((rd[i].S+query(rd[i].S))-rd[i].F);
		update(rd[i].S);
	}
	printf("%lld\n",ans);
	return 0;
}
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int bm = 200001;
int BIT[bm];
void add(int A) {
	while (A > 0) {
		BIT[A]++;
		A -= A & -A;
	}
}
int query(int A) {
	int ret = 0;
	while (A <= bm) {
		ret += BIT[A];
		A += A & -A;
	}
	return ret;
}
ll A[200001], B[200001], tmp[200001];
void pakuri_sort(int N, ll A[]) {
	const int b = 8;
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		if ((ct = *ci++) >= '0') {
			tmp = tmp * 10 + ct - '0';
			if (*ci++ == '0') {
				tmp = tmp * 10;
				ci++;
			}
		}
	}
	return tmp;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N = getint();
	rep1(i, N) A[i] = ll(i) << 32 | i + getint();
	rep1(i, N) B[i] = ll(i) << 32 | i + getint();

	pakuri_sort(N, A + 1);
	pakuri_sort(N, B + 1);

	rep1(i, N) {
		if ((A[i] ^ B[i]) & (1ll << 32) - 1) {
			co(-1);
			return 0;
		}
		tmp[A[i] >> 32] = B[i] >> 32;
	}
	ll kotae = 0;
	rep1(i, N) {
		kotae += query(tmp[i]);
		add(tmp[i]);
	}
	co(kotae);

	Would you please return 0;
}