//GIVE ME AC!!!!!!!!!!!!!!!!!
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll inf =1e18;
const ll MOD=1000000007;
const ll mod=998244353;
const double pi=acos(-1);
using P=pair<ll,ll>;
ll merge_cnt(vector<ll> &a) {
    ll n = a.size();
    if (n <= 1) { return 0; }
    ll cnt = 0;
    vector<ll> b(a.begin(), a.begin()+n/2);
    vector<ll> c(a.begin()+(n/2), a.end());
    cnt += merge_cnt(b);
    cnt += merge_cnt(c);
    ll ai = 0, bi = 0, ci = 0;
    // merge の処理
    while (ai < n) {
        if ( bi < b.size() && (ci == c.size() || b[bi] <= c[ci]) ) {
            a[ai++] = b[bi++];
        } else {
            cnt += n / 2 - bi;
            a[ai++] = c[ci++];
        }
    }
    return cnt;
}
int main(){
  int n;
  cin>>n;
  map<int,vector<int>>p;
  int a[n];
  rep(i,0,n){
    cin>>a[i];
    p[a[i]+i].push_back(i);
  }
  int b;
  bool ok=true;
  vector<ll>ans(n);
  rep(i,0,n){
    cin>>b;
    b+=i;
    if(p[b].size()==0){
      ok=false;
      break;
    }
    else{
      ans[p[b][0]]=i;
      p[b].erase(p[b].begin());
    }
  }
  if(!ok){
    cout<<-1<<endl;
    return 0;
  }
  cout<<merge_cnt(ans)<<endl;
}
/*Lucky_Glass*/
#include <cstdio>
#include <cstring>
#include <algorithm>

const int N = 2e5 + 10;
#define con(typ) const typ &

struct BITArray {
  int arr[N], siz;
  void modify(con(int) pos, con(int) key) {
    for (int i = pos; i <= siz; i += i & -i)
      arr[i] += key;
  }
  int query(con(int) pos) {
    int ans = 0;
    for (int i = pos; i; i -= i & -i)
      ans += arr[i];
    return ans;
  }
} bit;

int n;
std::pair<int, int> posa[N], posb[N];
int rec[N];

int main() {
  scanf("%d", &n);
  for (int i = 1, tmp; i <= n; ++i) {
    scanf("%d", &tmp);
    posa[i] = std::make_pair(tmp + i - 1, i);
  }
  for (int i = 1, tmp; i <= n; ++i) {
    scanf("%d", &tmp);
    posb[i] = std::make_pair(tmp + i - 1, i);
  }
  std::sort(posa + 1, posa + 1 + n);
  std::sort(posb + 1, posb + 1 + n);
  for (int i = 1; i <= n; ++i)
    if ( posa[i].first != posb[i].first ) {
      printf("-1\n");
      return 0;
    } else {
      rec[posa[i].second] = posb[i].second;
    }
  bit.siz = n;
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += bit.query(n - rec[i] + 1);
    bit.modify(n - rec[i] + 1, 1);
  }
  printf("%lld\n", ans);
  return 0;
}
