#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
//#define int long long
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define mod 998244353
struct modint{
    int x;
    modint(int o=0){x=o;}
    modint &operator = (int o){return x=o,*this;}
    modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
    modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
    modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
    modint &operator ^=(int b){
        modint a=*this,c=1;
        for(;b;b>>=1,a*=a)if(b&1)c*=a;
        return x=c.x,*this;
    }
    modint &operator /=(modint o){return *this *=o^=mod-2;}
    modint &operator +=(int o){return x=x+o>=mod?x+o-mod:x+o,*this;}
    modint &operator -=(int o){return x=x-o<0?x-o+mod:x-o,*this;}
    modint &operator *=(int o){return x=1ll*x*o%mod,*this;}
    modint &operator /=(int o){return *this *= ((modint(o))^=mod-2);}
    template<class I>friend modint operator +(modint a,I b){return a+=b;}
    template<class I>friend modint operator -(modint a,I b){return a-=b;}
    template<class I>friend modint operator *(modint a,I b){return a*=b;}
    template<class I>friend modint operator /(modint a,I b){return a/=b;}
    friend modint operator ^(modint a,int b){return a^=b;}
    friend bool operator ==(modint a,int b){return a.x==b;}
    friend bool operator !=(modint a,int b){return a.x!=b;}
    bool operator ! () {return !x;}
    modint operator - () {return x?mod-x:0;}
};

#define maxn 200005
int n,a[maxn],b[maxn],cnt; 
map<int,int>mp,mp2,id;
inline int ID(int x){
	if(!id.count(x))return id[x]=++cnt;
	return id[x];
}
inline int ABS(int x){
	return x<0?-x:x;
}
vector<int>va[maxn],vb[maxn];
int pa[maxn],pb[maxn],p[maxn],tmp[maxn];
long long tot;
int t[maxn];
void msort(int l,int r)
{
    if(l==r)return;
    int m=(l+r)/2;
    msort(l,m);
    msort(m+1,r);
    int p=l,i=l,j=m+1;
    while(i<=m&&j<=r)
    {
        if(a[i]>a[j])
        {
            tot+=m-i+1;
            t[p++]=a[j++];
        }
        else t[p++]=a[i++];
    }
    while(i<=m)t[p++]=a[i++];
    while(j<=r)t[p++]=a[j++];
    for(i=l;i<=r;i++)a[i]=t[i];
}
signed main()
{
	n=read();
	For(i,1,n)a[i]=read()+i,mp[a[i]]++,ID(a[i]);
	For(i,1,n)b[i]=read()+i,mp2[b[i]]++;
	for(auto it:mp)
		if(mp2[it.fi]!=it.se){
			puts("-1");
			return 0;
		}
	For(i,1,n)va[ID(a[i])].pb(i);
	For(i,1,n)vb[ID(b[i])].pb(i);
	long long res=0,now=0;
	For(i,1,cnt){
		int sz=va[i].size();
		For(j,0,sz-1){
			int u=++now;
			pa[va[i][j]]=u;
			pb[vb[i][j]]=u;
		}
	}
	For(i,1,n)tmp[pb[i]]=i;
	For(i,1,n)pa[i]=tmp[pa[i]],a[i]=pa[i];
	msort(1,n);
	cout<<tot;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

// 0-indexed
template <class T>
struct BIT {
  int treesize;
  vector<T> lst;
  // constructor
  BIT(int newn = 0) : treesize(newn), lst(newn + 1, 0) {}
  // a_place += num
  void add(int place, T num) {
    ++place;
    while (place <= treesize) {
      lst[place] += num;
      place += place & -place;
    }
  }
  // sum between [0,place)
  T sum(int place) {
    T res = 0;
    while (place > 0) {
      res += lst[place];
      place -= place & -place;
    }
    return res;
  }
  // sum [l,r)
  T sum(int left, int right) { return sum(right) - sum(left); }
};

int n;
vector<int> a, b;

long long solve();

int main() {
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i] += i;
  }
  b.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    b[i] += i;
  }
  cout << solve() << endl;
  return 0;
}

long long solve() {
  {
    vector<int> va = a, vb = b;
    sort(va.begin(), va.end());
    sort(vb.begin(), vb.end());
    for (int i = 0; i < n; ++i)
      if (va[i] != vb[i]) return -1;
    map<int, vector<int>> mp;
    for (int i = 0; i < n; ++i) mp[b[i]].push_back(i);
    for (int i = n - 1; i >= 0; --i) {
      long long now = a[i];
      a[i] = mp[now].back();
      mp[now].pop_back();
    }
  }
  long long res = 0;
  BIT<long long> bit(n);
  for (int i = 0; i < n; ++i) {
    res += bit.sum(a[i], n);
    bit.add(a[i], 1);
  }
  return res;
}