#include<stdio.h>
#include<stdlib.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static int IN(void)
{
  int x=0,f=1,c;while(c=getchar(),c<48||c>57){if(c==45)f=-f;}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f*x;
}
static void OUT(long x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x-x/10*10+48);}
static int Asc(const void *p1,const void *p2){rep(i,2){return ((int*)p1)[i]-((int*)p2)[i];}}
int main(void)
{
  int N=IN(),A[200000][2],B[200000][2],C[200000],bit[200000]={};long count=0l,sum=0l;
  rep(i,N){A[i][0]=IN()+i;A[i][1]=i;}qsort(A,N,sizeof(int*),Asc);
  rep(i,N){B[i][0]=IN()+i;B[i][1]=i;}qsort(B,N,sizeof(int*),Asc);
  rep(i,N){if(A[i][0]!=B[i][0]){return !puts("-1");}C[A[i][1]]=B[i][1];}
  rep(i,N)
  {
    sum=0l;for(int j=C[N-1-i]-1;j>=0;j=(j&(j+1))-1){sum+=bit[j];}count+=sum;
    for(int j=C[N-1-i];j<N;j|=j+1){bit[j]++;}
  }
  OUT(count);
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=5e5+500;
int n,a[N],b[N],tmpa[N],tmpb[N],cur[N],p[N],s[N];
vector<int>pos[N];
int query(int x){int ans=0;for(;x;x-=x&-x)ans+=s[x];return ans;}
void ins(int x){for(;x<=n;x+=x&-x)s[x]++;}
int main()
{
	long long ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i),a[i]+=i,tmpa[i]=a[i];
	for(int i=1;i<=n;i++)scanf("%d",b+i),b[i]+=i,tmpb[i]=b[i];
	sort(tmpa+1,tmpa+n+1),sort(tmpb+1,tmpb+n+1);
	for(int i=1;i<=n;i++)if(tmpa[i]!=tmpb[i]){puts("-1");return 0;}
	for(int i=1;i<=n;i++)a[i]=lower_bound(tmpa+1,tmpa+n+1,a[i])-tmpa;
	for(int i=1;i<=n;i++)b[i]=lower_bound(tmpb+1,tmpb+n+1,b[i])-tmpb;
	for(int i=1;i<=n;i++)pos[b[i]].push_back(i);
	for(int i=1;i<=n;i++)p[i]=pos[a[i]][cur[a[i]]],cur[a[i]]++;
	for(int i=n;i>=1;i--)ans+=query(p[i]),ins(p[i]);
	cout<<ans<<endl;
}