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

#define ll  long long
#define ull  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
const ll maxn =5e5+5;
const ll mod=998242361 ;
const ll base=1e18;
map<ll,vector<ll>> mp;
ll a[maxn];
ll b[maxn];
ll fwt[2*maxn];
ll n;
ll cnt;
void update(ll l, ll r, ll d) {
    for (int p = l; p <= cnt; p += p & -p) fwt[p] = (fwt[p] + d) ;
    ++r;
    for (int p = r; p <= cnt; p += p & -p) fwt[p] = (fwt[p] - d) ;
}
ll get(ll p) {
    ll ret = 0;
    for (; p; p -= p & -p) ret = (ret + fwt[p]) ;
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (fopen("t.inp","r"))
    {
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }

     cin>>n ;
     cnt=n;
     for (int i=1;i<=n;i++)
     {
         cin>>a[i];
         update(i,i,i);
     }
     for (int i=n;i>=1;i--) mp[a[i]+i-1].pb(i);
     for (int i=1;i<=n;i++)
     {
         cin>>b[i];
     }
     ll ans=0;
     for (int i=1;i<=n;i++)
     {
         if (mp[b[i]+i-1].size())
         {

              ans=(ans+abs(i-get(mp[b[i]+i-1].back())));
              update(1,mp[b[i]+i-1].back(),1);
              mp[b[i]+i-1].pop_back();

         }
         else
         {
             //cout <<i<<endl;
             cout <<-1;
             return 0;
         }
     }
     cout <<ans;

}

#include "bits/stdc++.h"
#include <chrono>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>


#define INF 1000000007
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,fma,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std; 






typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef unordered_map<int,int> umi ;
typedef unordered_set<int> usi ;
typedef pair<int,int> pi;
typedef tree< pair<ll,ll>,null_type, less<pair<ll,ll>> ,rb_tree_tag,tree_order_statistics_node_update > indexed_set;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

bool ldequal(ld  a ,ld b){
    return abs(a-b) < 1e-9  ;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(permutation.begin(), permutation.end(), rng); for permute 
// uniform_int_distribution<int>(0, i)(rng) for rand

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif



const int N =2e5+1 ;
ll a[N] ,b[N] ; 
indexed_set s1 ;
set<pair<ll,ll>> s2 ;

bool tests=false ;
void gen(){} ; 

void solve(){
   int n ;
   cin>>n ;
   REP(i,0,n){
    cin>>a[i] ;
    s1.insert({i,a[i]+i}) ;
    s2.insert({a[i]+i,i}) ;
   }
   REP(i,0,n) cin>>b[i] ; 
   ll res=0 ;
   REP(i,0,n){
      ll val =b[i]+i ;
      auto l = s2.lower_bound({val,0}) ;
      if(l==s2.end()){
         cout<<-1 ;
         return ;
      }
      pair<ll,ll> temp = MP((*l).S ,(*l).F) ;
      auto r =s1.lower_bound(temp)  ;
      debug(val,*l ,*r , s1.order_of_key(*r) ) ;
      res+=s1.order_of_key(*r) ;
      s2.erase(l) ;
      s1.erase(r) ;
   }
   cout<<res ;

}


 

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    gen() ;
    long long test =1 ;
    if(tests) cin>>test ;
    REP(i,1,test+1){
        debug(i) ;
        //cout<<"Case #"<<i<<": " ;

        solve() ;
        cout<<"\n" ;
    }
    return 0 ;
}