/**
  * @brief atcoder
  * @author yao
  */
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <utility>
#include <algorithm>
#include <functional>
#include <climits>
#include <vector>
#include <map>
#define ft first
#define sd second
#ifdef DBG
#   define dbg_pri(x...) fprintf(stderr,x)
#else
#   define dbg_pri(x...) 0
#   define NDEBUG
#endif //DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 262144

static_assert(__builtin_popcount(N) == 1);
template <class T>
class Bit{
public:
    T a[N];
    void add(int x,T k)
    {
        while( x < N ) a[x] += k, x += x+1&~x;
    }
    T qry(int x)
    {
        T res = 0;
        while( x >= 0 ) res += a[x], x -= x+1&~x;
        return res;
    }
    int lbd(T k)
    {
        if( a[N-1] < k ) return N;
        int res = -1, x = N>>1;
        while(x)
        {
            if(a[res+x]<k) k-=a[res+=x];
            x>>=1;
        }
        return res+1;
    }
    int ubd(T k)
    {
        if( a[N-1] <= k ) return N;
        int res = -1, x = N>>1;
        while(x)
        {
            if(a[res+x]<=k) k-=a[res+=x];
            x>>=1;
        }
        return res+1;
    }
};

Bit<int> bit;

int a[N], b[N];
std::map<int, std::vector<int>> mp;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<n;++i) scanf("%d", &a[i]);
    for(int i=0;i<n;++i) scanf("%d", &b[i]);
    for(int i=n-1;i>=0;--i) mp[b[i]+i].push_back(i);
    lli ans = 0;
    for(int i=0;i<n;++i)
    {
        std::vector<int> &v = mp[a[i]+i];
        if(v.empty()) return puts("-1"), 0;
        int k = v.back();
        v.pop_back();
        ans += i-bit.qry(k);
        bit.add(k,1);
    }
    printf("%lld\n", ans);
    return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<numeric>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
//#define MOD 1000000007
#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
#define PI acos(-1.0)

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;
//using mint = modint998244353;

int dh[] = {-1,0,1,0};
int dw[] = {0,1,0,-1};

template<typename T>
struct BinaryIndexedTree{
	vector<T> bit;
	BinaryIndexedTree(int sz){
		bit.resize(sz+1);
	}
	void add(int k, T x){ //k番目にxを加える
		for(int i = k; i < (int)bit.size(); i |= i+1) bit[i] += x;	
	}
	T sum(int k){ //[0,k)の区間の総和を求める
		T res = 0;      
		for(int i = k-1; i >= 0; i = (i&(i+1))-1){
			res += bit[i];
		}
		return res;
	}
};

int main(){
    int n; cin >> n;
    vector<Pi> a(n);
    vector<Pi> b(n);
    rep(i,n) cin >> a[i].first,a[i].second = i;
    rep(i,n) cin >> b[i].first,b[i].second = i;
    rep(i,n) a[i].first += i;
    rep(i,n) b[i].first += i;
    vector<Pi> c,d;
    c = a,d = b;
    sort(c.begin(),c.end());
    sort(d.begin(),d.end());
    rep(i,n){
        if(c[i].first != d[i].first){
            cout << -1 << endl;
            return 0;
        }
    }
    vector<int> id(n);
    rep(i,n) id[c[i].second] = d[i].second; 
    ll ans = 0;
    BinaryIndexedTree<int> bit(n);
    rep(i,n){
        ans += bit.sum(n)-bit.sum(id[a[i].second]);
        bit.add(id[a[i].second],1);
    }
    cout << ans << endl;
}


