// ＵＴＦ−８

#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<double>;

/* #include<atcoder/all> */
/* using namespace atcoder; */

template<class T>bool chmax(T &a, const T &b) { return (a<b ? (a=b,1) : 0); }
template<class T>bool chmin(T &a, const T &b) { return (a>b ? (a=b,1) : 0); }

template <typename T>
struct SegmentTree {
    using F = function<T(T, T)>;

    const T e;
    const F f;
    size_t sz;
    vector<T> tree;

    SegmentTree(size_t n, const F &f, const T &e = 0) : f(f), e(e) {
        sz = 1;
        while(sz < n) sz <<= 1;
        tree.assign(2*sz, e);
    }

    void set(typename vector<T>::iterator begin, typename vector<T>::iterator end) {
        copy(begin, end, tree.begin() + sz);
        for(size_t k=sz-1; k>0; k--)
            tree[k] = f(tree[2*k+0], tree[2*k+1]);
    }

    void update(size_t k, const T &x) {
        k += sz;
        tree[k] = x;
        while(k >>= 1)
            tree[k] = f(tree[2*k+0], tree[2*k+1]);
    }

    T query(size_t a, size_t b) const {
        b = min(b, sz);
        a = max((size_t)0, a);
        T l = e, r = e;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) l = f(l, tree[a++]);
            if(b & 1) r = f(tree[--b], r);
        }
        return f(l, r);
    }

    T operator[](const size_t k) const {
        return tree[sz + k];
    }
};

int main(void) {
    constexpr ll MOD = 0 ? 1e9+7 : 998244353;
    const double PI = acos(-1);
    constexpr double eps = 1e-10;
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);


    if(1) {
        ll n;
        cin >> n;
        vector<ll> a(n), b(n), c(n);
        for(auto &e: a) cin >> e;
        for(auto &e: b) cin >> e;

        map<ll,vector<ll>> m;
        for(ll i=0; i<n; i++)
            m[a[i]+i].push_back(i);
        map<ll,ll> idx;

        for(ll i=0; i<n; i++) {
            ll x = b[i]+i;
            auto &v = m[x];
            ll y = idx[x]++;
            if(y == v.size()) {
                cout << -1 << endl;
                return 0;
            }
            c[i] = v[y];
        }

        ll r = 0;
        SegmentTree<ll> st(n, [&](ll a, ll b){return a + b;}, 0);
        for(ll i=0; i<n; i++) {
            r += i - st.query(0, c[i]);
            st.update(c[i], st[c[i]]+1);
        }

        cout << r << endl;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
typedef pair<int, int> pii;
#define mp(a, b) make_pair(a, b)
int n;
int a[maxn], b[maxn], c[maxn], d[maxn], p[maxn], tree[maxn];
bool cmpa(const int & i, const int & j)
{
	return a[i] == a[j] ? i < j : a[i] < a[j];
}
bool cmpb(const int & i, const int & j)
{
	return b[i] == b[j] ? i < j : b[i] < b[j];
}
inline int lowbit(int x) { return (x & (-x));}
ll sum(int x) { int ret = 0; for(int i = x; i < maxn; i += lowbit(i)) ret += tree[i]; return ret;}
void add(int x) { for(int i = x; i; i -= lowbit(i)) ++tree[i];}
ll invpair()
{
	ll ret = 0;
	for(int i = 1; i <= n; ++i) 
	{
		ret += sum(p[i]);
		add(p[i]);
	}
	return ret;
}
int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[i] += i, c[i] = i;
	for(int i = 1; i <= n; ++i) scanf("%d", &b[i]), b[i] += i, d[i] = i;
	sort(c + 1, c + n + 1, cmpa);
	sort(d + 1, d + n + 1, cmpb);
	for(int i = 1; i <= n; ++i)
		if(a[c[i]] != b[d[i]])
		{
			puts("-1");
			return 0;
		}
	for(int i = 1; i <= n; ++i) p[d[i]] = c[i];
	printf("%lld\n", invpair());
	return 0;
}