#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#define ll long long
#define pb push_back
#define forn(i,x,n) for(int i=x;i<=n;++i)
#define forr(i,x,n) for(int i=n;i>=x;--i)
#define lson (rt<< 1)
#define rson (rt<< 1 | 1)
#define gmid ((l+r)>> 1 )
using namespace std;
const int maxn=20000050;

struct node 
{
	int v;int pos;
}aa[maxn],bb[maxn];

int n,s[maxn],a[maxn],b[maxn],c[maxn];

bool cmp(node a,node b)
{
	return a.v<b.v||a.v==b.v&&a.pos<b.pos;
}

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

int query(int x)
{
	int ans=0;
	while(x)
	{
		ans+=s[x];
		x-=lowbit(x);
	}
	return ans;
}

void insert(int x)
{
	while(x<=n)
	{
		s[x]++;
		x+=lowbit(x);
	}
}

int main()
{
	cin>>n;
	forn(i,1,n)
	  {
	  	  cin>>a[i];
	  	  aa[i].v=a[i]+i;
	  	  aa[i].pos=i;
	  }
	forn(i,1,n)
	  {
	  	  cin>>b[i];
	  	  bb[i].v=b[i]+i;
	  	  bb[i].pos=i;
	  }  
	sort(aa+1,aa+n+1,cmp);
	sort(bb+1,bb+n+1,cmp);
	bool flag=true;
	for(int i=1;i<=n;++i)
	  if(aa[i].v!=bb[i].v)flag=false;
	if(flag==false) cout<<-1<<endl;
	else
	{
		ll ans=0;
		forn(i,1,n)
		  c[aa[i].pos]=bb[i].pos;
		forn(i,1,n)
		{
			ans+=query(n)-query(c[i]);
			insert(c[i]);
		}  
		cout<<ans<<endl;
	}    
}

#include <bits/stdc++.h>
#define No return puts("-1"), 0
using namespace std;

typedef long long i64;
const int N = 2e5+10;
int n, m, a[N], b[N], c[N], to[N], bt[N];
vector<int> da[N], db[N];
void add(int x, int v=1) { for(; x <= n; x += x&-x) bt[x] += v; }
int ask(int x) { int y = 0; for(; x; x -= x&-x) y += bt[x]; return y; }

signed main() {
	cin >> n;
	for(int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[i] += i, c[i] = a[i];
	for(int i = 1; i <= n; i ++) scanf("%d", &b[i]), b[i] += i;
	sort(c+1, c+1+n); m = unique(c+1, c+1+n) - c-1;
	for(int i = 1; i <= n; i ++) {
		a[i] = lower_bound(c+1, c+1+m, a[i]) - c;
		int p = lower_bound(c+1, c+1+m, b[i]) - c;
		if(c[p] != b[i]) No;
		da[a[i]].push_back(i), db[p].push_back(i);
	}
	for(int i = 1; i <= m; i ++) {
		if(da[i].size() != db[i].size()) No;
		for(int j = 0; j < da[i].size(); j ++) 
			to[da[i][j]] = db[i][j];
	}
	i64 ans = 0;
	for(int i = n; i; i --) {
		ans += ask(to[i]);
		add(to[i]);
	}
	cout << ans << endl;
	return 0;
}