//红太阳zhouakngyang txdy!
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include<bits/stdc++.h>
#define Macesuted cout<<"kawaii"<<endl
#define MatrixCascade cout<<"npsl"<<endl
#define Karry5307 cout<<"nmsl"<<endl
using namespace std;
#define int long long
inline int read()
{
	int sum=0,nega=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')nega=-1;ch=getchar();}
	while(ch<='9'&&ch>='0')sum=sum*10+ch-'0',ch=getchar();
	return sum*nega;
}
const int N=2e5+9,mod=998244353,M=4e5+9;
int n,a[N],b[N],ans,c[M];
struct point
{
	int x,pos;
}ax[N],bx[N];
map <int,int> mp;
int tim;
vector<int> pa[N],pb[N];
inline int lowbit(int x)
{
	return x&(-x);
}
inline void add(int x,int k)
{
	while(x<=M)
	{
		c[x]+=k;x+=lowbit(x);
	}
}
inline int query(int x)
{
	int res=0;
	while(x!=0)
	{
		res+=c[x];x-=lowbit(x);
	}
	return res;
}
signed main()
{
	n=read();
	for(int i=1;i<=n;i++)a[i]=read(),ax[i].x=a[i]+i-1,ax[i].pos=i;
	for(int i=1;i<=n;i++)b[i]=read(),bx[i].x=b[i]+i-1,bx[i].pos=i;
	for(int i=1;i<=n;i++)
		if(!mp[ax[i].x])mp[ax[i].x]=++tim;
	for(int i=1;i<=n;i++)pa[mp[ax[i].x]].push_back(i);
	for(int i=1;i<=n;i++)
	{
		if(!mp[bx[i].x])
		{
			puts("-1");return 0;
		}
		pb[mp[bx[i].x]].push_back(i);
	}
	for(int i=1;i<=tim;i++)
	{
		if(pa[i].size()!=pb[i].size())
		{
			puts("-1");return 0;
		}
		for(int j=0;j<pa[i].size();j++)
		{
			a[pa[i][j]]=pb[i][j];
		}
	}
//	for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
	for(int i=1;i<=n;i++)
	{
		ans+=(i-1-query(a[i]));
		add(a[i],1);
	//	cout<<ans<<endl;
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

int N=1<<18;
class StarrySkyTree{
public:
  ll Mi[555555],A[555555];
  void init(){memset(Mi,0,sizeof(Mi)),memset(A,0,sizeof(A));}
  void add(int a,int b,ll x,int k=0,int l=0,int r=N) {
    if(r<=a||b<=l) return;
    if(a<=l&&r<=b){
      A[k]+=x;
      while(k){
        k=(k-1)/2;
        Mi[k]=min(Mi[k*2+1]+A[k*2+1],Mi[k*2+2]+A[k*2+2]);
      }
      return;
    }
    add(a,b,x,k*2+1,l,(l+r)/2);
    add(a,b,x,k*2+2,(l+r)/2,r);
  }
  ll getMin(int a,int b,int k=0,int l=0,int r=N) {
    if(r<=a||b<=l)return MAXL;if(a<=l&&r<=b)return Mi[k]+A[k];
    ll left=getMin(a,b,k*2+1,l,(l+r)/2),right=getMin(a,b,k*2+2,(l+r)/2,r);
    return min(left,right)+A[k];
  }
};

StarrySkyTree t;

void Main() {
  ll n;
  R n;
  ll a[n],b[n];
  rep(i,n) R a[i];
  rep(i,n) R b[i];
  ll s1=0,s2=0;
  rep(i,n) {
    s1+=a[i];
    s2+=b[i];
  }
  if(s1!=s2) {
    pr(-1);
    return;
  }
  rep(i,n) {
    a[i]+=i;
    b[i]+=i;
  }
  t.init();
  map<ll,queue<ll> > m;
  rep(i,n) m[a[i]].push(i);
  ll ans=0;
  rep(i,n) {
    if(m[b[i]].empty()) {
      pr(-1);
      return;
    }
    ll k=m[b[i]].front();m[b[i]].pop();
    ll x=k+t.getMin(k,k+1);
    ans+=x-i;
    t.add(0,k,1);
  }
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
