#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=200005;

int n,a[N],b[N],c[N],t[N];
map<int,priority_queue<int,vector<int>,greater<int> > > pq;
int main(){
	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;
	for(int i=1;i<=n;i++) pq[a[i]].push(i);
	for(int i=1;i<=n;i++){
		if(pq[b[i]].empty()){
			cout<<"-1\n";
			return 0;
		}
		int pv=pq[b[i]].top();
		pq[b[i]].pop();
		c[i]=pv;
	}
	LL ans=0LL;
	for(int i=n;i;i--){
		for(int j=c[i];j;j-=(j&-j)) ans+=1LL*t[j];
		for(int j=c[i];j<=n;j+=(j&-j)) t[j]++;
	}
	cout<<ans<<'\n';
	return 0;	
}
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#include<map>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
template<class T> struct Fenwick {
    int n;
    T* d;
    Fenwick() : n(0), d(NULL) {}
    Fenwick(int n_) : n(n_) {
	d = new T[n_]();
    }
    Fenwick(const Fenwick &y) : n(y.n) {
	d = new T[n];
	memcpy(d, y.d, sizeof (T) * n);
    }
    ~Fenwick() {
	delete[] d; d = NULL;
	n = 0;
    }
    friend void swap(Fenwick &x, Fenwick &y) {
	swap(x.n, y.n); swap(x.d, y.d);
    }
    Fenwick& operator=(Fenwick y) {
	swap(*this, y);
	return *this;
    }
    inline void add(int i, const T &x) {
	for (; i<n; i|=i+1) d[i] += x;
    }
    inline T sum(int r) const {
	T s = T();
	for (; r; r&=r-1) s += d[r-1];
	return s;
    }
    T sum(int l, int r) const {
	return sum(r) - sum(l);
    }
};

int N;
LL A[200011];
LL B[200011];

bool valid() {
    vector<LL> X(A, A+N);
    vector<LL> Y(B, B+N);
    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    return X == Y;
}

void MAIN() {
    scanf("%d", &N);
    REP (i, N) scanf("%lld", A+i);
    REP (i, N) scanf("%lld", B+i);

    REP (i, N) {
	A[i] += i;
	B[i] += i;
    }

    if (valid()) {
	map<LL, int> cnt;
	map<pair<LL, int>, int> mp;

	REP (i, N) {
	    int k = cnt[A[i]];
	    mp[make_pair(A[i], k)] = i;
	    cnt[A[i]]++;
	}

	cnt.clear();
	LL ans = 0;
	Fenwick<int> F(N);
	REP (i, N) {
	    int k = cnt[B[i]];
	    int pos = mp[make_pair(B[i], k)];
	    cnt[B[i]]++;
	    ans += F.sum(pos, N);
	    F.add(pos, 1);
	}
	printf("%lld\n", ans);
    } else {
	puts("-1");
    }

}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

