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

typedef long long ll;
#ifdef LOCAL
    const int N = 105;
#else
    const int N = 2e5 + 105;
#endif

#define mid (l + r >> 1)
#define ls (rt << 1)
#define rs (rt << 1 | 1)
const int M = N + N;
int a[N], b[N], n;
int tr[M << 2];

void init(){
    set<int> se;
    for(int i = 1; i <= n; ++i) cin >> a[i], se.insert(a[i] + i);
    for(int i = 1; i <= n; ++i) cin >> b[i], se.insert(b[i] + i);
    map<int, int> ma;
    int tot = 0;
    for(auto it : se){
        ma[it] = ++tot;
    }
    for(int i = 1; i <= n; ++i) a[i] = ma[a[i] + i];
    for(int i = 1; i <= n; ++i) b[i] = ma[b[i] + i];
}
set<int>pos[N + N];
int p[N];
void add(int rt, int l, int r, int idx){
    if(l == r) { tr[rt]++; return; }
    if(idx <= mid) add(ls, l, mid, idx);
    else add(rs, mid + 1, r, idx);
    tr[rt] = tr[ls] + tr[rs];
}
int qr(int rt, int l, int r, int L ,int R){
    int res = 0;
    if(L <= l && R >= r){ return tr[rt]; }
    if(L <= mid)  res += qr(ls, l, mid, L, R);
    if(R > mid) res += qr(rs, mid + 1, r, L, R);
    return res;
}
void solve() {
    cin >> n;
    init();
    for(int i = 1; i <= n; ++i) pos[a[i]].insert(i);
    for(int i = 1; i <= n; ++i){
        if(pos[b[i]].size() == 0) {
            cout << -1 << endl; return;
        }
        auto it = pos[b[i]].begin();
        p[i] = *it;
        pos[b[i]].erase(it);
    }
    ll ans = 0;
    for(int i = 1; i <= n; ++i){
        ans += qr(1, 1, n, p[i], n);
        add(1, 1, n, p[i]);
    }
    cout << ans << endl;
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    cout << "start Debug" << endl;
#endif
    ios::sync_with_stdio(0), cin.tie(0);
    int _ = 1;
    // cin >> _;
    for(int i = 1; i <= _; ++i) solve(); 
}
#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);
  
}
