#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 505;
char en = '\n';
ll inf = 1e16;
ll mod = 998244353;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

using namespace __gnu_pbds; //(or pb_ds)
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

char arr[N][N];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;

  ll a[n + 5];
  ll b[n + 5];

  map<ll, vector<ll>> indices;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    indices[i + a[i]].push_back(i);
  }
  for (ll i = 1; i <= n; i++) {
    cin >> b[i];
  }

  for (auto &it : indices) {
    reverse(it.second.begin(), it.second.end());
  }
  pbds X;
  ll res = 0;

  for (ll i = 1; i <= n; i++) {
    ll req = b[i] + i;
    if (indices.find(req) == indices.end()) {
      cout << -1 << en;
      return 0;
    }
    vector<ll> &vect = indices[req];
    ll indx = vect.back();
    vect.pop_back();
    if (vect.empty()) {
      indices.erase(req);
    }

    // adjust for changes
    // find indices > indx
    ll change = (i - 1) - X.order_of_key(indx);

    X.insert(indx);
    res += indx + change - i;
  }
  cout << res << en;

  return 0;
}

#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define neinf 0xc0c0c0c0c0c0c0c0ll
#define inf 0x3f3f3f3f3f3f3f3fll
#define uint unsigned int
#define ull unsigned ll
#define HgS 1000000007
#define ll long long
#define reg register
#define opr operator
#define ret return
#define gc getchar
#define pc putchar
#define cst const
#define db double
#define il inline
il ll rd()
{
	reg ll res=0,lab=1;
	reg char ch=gc();
	while((ch<'0'||ch>'9')&&ch!=EOF)
	{if(ch=='-')lab=-lab;ch=gc();}
	while(ch>='0'&&ch<='9')
		res=(res<<3)+(res<<1)+(ch&15),ch=gc();
	return res*lab;
}
il void prt(ll x,char t='\n')
{
	static char ch[40];reg int tp=0;
	if(!x){fputs("0",stdout);if(t)pc(t);return;}
	if(x<0)pc('-'),x=-x;
	while(x)ch[++tp]=(x%10)^48,x/=10;
	while(tp)pc(ch[tp--]);
	if(t)pc(t);
}
il ll umax(ll a,ll b){return a>b?a:b;}
il ll umin(ll a,ll b){return a<b?a:b;}
il ll uabs(ll x){return x>0?x:-x;}
il ll qpow(ll n,ll e=HgS-2,ll p=HgS)
{
	reg ll res=1;
	while(e){if(e&1)res=res*n%p;n=n*n%p;e>>=1;}
	return res;
}
ll n,a[200005],b[200005],ans,rf[200005],res[200005],cnt,bit[200005];
unordered_map<ll,ll>tmp,tmp2;
queue<ll>q[200005];
//void m_sort(int l,int r)
//{
//	if(l==r)return;
//	int mid=(l+r)>>1,i=l,j=mid+1,k=l;
//	m_sort(l,mid);m_sort(mid+1,r);
//	while(i<=mid&&j<=r)
//		if(a[i]<=a[j])res[k++]=a[i++];
//		else res[k++]=a[j++],ans+=mid-i+1;
//	while(i<=mid)res[k++]=a[i++];
//	while(j<=r)res[k++]=a[j++];
//	for(int t=l;t<=r;++t)a[t]=res[t];
//}
il int lowbit(int x){return x&(-x);}
void add(int x){while(x<=n)++bit[x],x+=lowbit(x);}
int query(int x)
{
	int res=0;
	while(x)res+=bit[x],x-=lowbit(x);
	return res;
}
int main()
{
	n=rd();
	for(int i=1;i<=n;++i)++tmp[a[i]=rd()-n+i];
	for(int i=1;i<=n;++i)--tmp[b[i]=rd()-n+i];
	for(int i=1;i<=n;++i)if(tmp[a[i]]||tmp[b[i]]){puts("-1");return 0;}
//	for(int i=1;i<=n;++i)cerr<<a[i]<<' ';
//	cerr<<endl;
//	for(int i=1;i<=n;++i)cerr<<b[i]<<' ';
//	cerr<<endl;
	for(int i=1;i<=n;++i)
	{
		if(tmp2.find(b[i])==tmp2.end())tmp2[b[i]]=++cnt,rf[cnt]=b[i];
		q[tmp2[b[i]]].push(i);
//		cerr<<tmp2[b[i]]<<' ';
	}
//	cerr<<endl;
//	for(int i=1;i<=n;++i)a[i]=q[tmp2[a[i]]].front(),q[tmp2[a[i]]].pop();
//	for(int i=1;i<=n;++i)cerr<<a[i]<<' ';
//	cerr<<endl;
//	m_sort(1,n);
	for(int i=1;i<=n;++i)
	{
		int val=q[tmp2[a[i]]].front();q[tmp2[a[i]]].pop();
		ans+=query(n)-query(val);add(val);
	}
	prt(ans);
	return 0;
}
