#include <bits/stdc++.h>
#define pb(x) push_back(x)
using namespace std;
using ll=long long;
const int N=300005;
const ll M=998244353;
int a[N],b[N],c[N];
map<int,int> g;
vector<int> f[N];
int gt=0,n;
void upd(int x){for(;x<=n;x+=x&-x)c[x]++;}
int q(int x){int res=0;for(;x;x-=x&-x)res+=c[x];return res;}
void f1(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i]+=i;}
	for(int i=1;i<=n;i++){
		scanf("%d",&b[i]);b[i]+=i;
		if(!g.count(b[i])){g[b[i]]=++gt;f[gt].pb(0);}
		f[g[b[i]]].pb(i);
	}
	for(int i=1;i<=n;i++){
		if(!g.count(a[i])){printf("-1");return ;}
		int na=g[a[i]];
		++f[na][0];
		if(f[na][0]>f[na].size()-1){printf("-1");return ;}
		a[i]=f[na][f[na][0]];
		
	}
	ll ans=0;
	for(int i=n;i>=1;i--){
		ans+=q(a[i]-1);
		upd(a[i]);
	}
	printf("%lld",ans);
}
int main(){
	f1();
	return 0;
}

/*

*/
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

#include <map>
#include <deque>

#include <functional>
template<typename T>
class SegmentTreeOneAll {
    using Func = function<T(T, T)>;

public:
    vector<T> data;
    int n;
    T init;
    Func update_func;
    Func query_func;
    SegmentTreeOneAll(int _n, T _init, Func up, Func qu) {
        init = _init;
        update_func = up;
        query_func = qu;
        for (n = 1; n < _n; n *= 2)
            ;
        data.resize(2 * n - 1, init);
    }
    void update(int pos, T val) {
        pos += n - 1;
        data[pos] = update_func(data[pos], val);
        while (pos > 0) {
            pos = (pos - 1) / 2;
            data[pos] = query_func(data[2 * pos + 1], data[2 * pos + 2]);
        }
    }
    T query(int l, int r) {
        T resL = init, resR = init;
        for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) {
            if (!(l & 1)) {
                resL = query_func(resL, data[l]);
            }
            if (!(r & 1)) {
                resR = query_func(data[r - 1], resR);
            }
        }
        return query_func(resL, resR);
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    input(n);
    vector<int> a(n), b(n);
    map<int, deque<int>> m;
    rep(i, 0, n) {
        input(a[i]);
        a[i] += i;
        m[a[i]].push_back(i);
    }
    vector<int> idx(n, -1);
    rep(i, 0, n) {
        input(b[i]);
        b[i] += i;
        if (m[b[i]].size()) {
            int index = m[b[i]].front();
            m[b[i]].pop_front();
            idx[index] = i;
        } else {
            print(-1);
            return 0;
        }
    }
    rep(i, 0, n) {
        if (idx[i] == -1) {
            print(-1);
            return 0;
        }
    }

    auto SUM = [](ll s, ll t) { return s + t; };
    SegmentTreeOneAll<ll> seg(n, 0LL, SUM, SUM);
    ll ans = 0;
    rep(i, 0, n) {
        int val = idx[i];
        ans += seg.query(val, n);
        seg.update(val, 1);
    }
    print(ans);
    return 0;
}
