#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long LL;
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define sz(v) (int)(v).size()
#define rep(i, x, y) for(int i = x, i##end = y; i <= i##end; ++i)
#define per(i, x, y) for(int i = x, i##end = y; i >= i##end; --i)
template<class T> inline bool ckmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
template<class T> inline bool ckmin(T &x, T y) { return x > y ? x = y, 1 : 0; }
inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(!isdigit(ch)) { if(ch == '-') f = 0; ch = getchar(); }
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return f ? x : -x;
}

const int N = 200005;
LL ans;
int n, a[N], b[N], lsh[N], rag, it[N], tr[N];
vector<int> va[N], vb[N];
inline void add(int x, int d) {
	for(int i = x; i <= n; i += i & -i) tr[i] += d;
}
inline int ask(int x) {
	int res = 0;
	for(int i = x; i > 0; i -= i & -i) res += tr[i];
	return res;
}
signed main() {
	n = read();
	rep(i, 1, n) a[i] = read() + i, lsh[++rag] = a[i];
	rep(i, 1, n) b[i] = read() + i;
	sort(lsh + 1, lsh + rag + 1), rag = unique(lsh + 1, lsh + rag + 1) - lsh - 1;
	rep(i, 1, n) a[i] = lower_bound(lsh + 1, lsh + rag + 1, a[i]) - lsh, va[a[i]].pb(i);
	rep(i, 1, n) {
		int tmp = b[i]; 
		b[i] = lower_bound(lsh + 1, lsh + rag + 1, b[i]) - lsh;
		if(lsh[b[i]] != tmp) return puts("-1"), 0;
		vb[b[i]].pb(i);
	}
	rep(i, 1, rag) if(sz(va[i]) != sz(vb[i])) return puts("-1"), 0;
	rep(i, 1, n) {
		int tmp = va[b[i]][it[b[i]]++];
		add(tmp, 1);
		tmp += i - ask(tmp);
		ans += tmp - i;
	}
	cout << ans << '\n';
}
#include<iostream>
#include<list>
#include<map>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
};

// pair 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){
  cout << "(" << p.first << "," << p.second << ")";
  return os;
}

// map 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const map<S,T>& mp){
  for(const pair<S,T>& x: mp){
    cout << "[" << x.first << "]=" << x.second << " ";
  }
  return os;
}

struct segtree{
  int n;
  vector<int> vec;
  
  segtree(int nn){
    n=nn;
    vec.resize(2*n,0);
  }
  
  int get(int i){ return vec[i+n]; };
  void set(li a,int i){
    i+=n;
    vec[i]=a;
    while(i>1){
      i/=2;
      vec[i]=vec[i*2]+vec[i*2+1];
    }
      
  };
  int sum(int i,int j){
    // [i,j)
    if(df)print(i,j);
    int s=0;
    i=i+n; j=j+n;
    
    while(i<j){
      if(i%2){ if(df) print(i); s+=vec[i]; i++; }
      if(j%2){ j--; s+=vec[j]; if(df) print(j); }
      i/=2; j/=2;
    }
    return s;
  }
  
};

std::ostream& operator<<(std::ostream& os, const segtree& s){
  rep(i,2*s.n) printf("%ld ",s.vec[i]);
  return os;
}


int judge(map<li,int>& mpa,map<li,int>& mpb){
  int f=1;
  int t=0;
  for(auto& x:mpa){
    if(mpb[x.first]!=x.second) f=0;
    if(df)print(x,mpb[x.first],"->",f);
  }
  return f;
}


int main(){
  int n; cin >>n;
  vector<li> a(n);
  map<li,int> mpa;
  rep(i,n){
    li c; cin >>c;
    a[i]=c+i;
    mpa[a[i]]++;
  }
  if(df)print(mpa);
  vector<li> b(n);
  map<li,int> mpb;
  rep(i,n){
    li c; cin >>c;
    b[i]=c+i;
    mpb[b[i]]++;
  }
  if(df)print(mpb);
  
  if(!judge(mpa,mpb)){
    print(-1);
    return 0;
  }
  map<li,list<int>> mp;
  rep(i,n){
    mp[b[i]].push_back(i);
  }
  if(df)print(mp);
  rep(i,n){
    int t=mp[a[i]].front();
    mp[a[i]].pop_front();
    a[i]=t;
  }
  if(df)print(a);
  
  segtree sgt(n);
  li ans=0;
  rep(i,n){
    ans+=sgt.sum(a[i],n);
    sgt.set(1,a[i]);
    if(df)print(sgt, sgt.sum(a[i],n));
  }
  print(ans);
  
}
