#include<cstdio> 
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<iostream>
#include<bitset>
#include<stack>
const int inf = 0x3f3f3f3f;//1.06e9大小
const double PI = acos ( -1 );
const double eps = 1e-6;
typedef unsigned long long ull;
typedef long long ll;
#define pii pair<int ,int>
#define X first
#define Y second
#define lowbit(a) a&(-a)
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=2e5+5;
map<int,stack<int>> mp;
int n;
int f[N],a[N];
void add(int x){
	for (int i=x;i<=n;i+=lowbit(i))
		f[i]++;
}
int query(int x){
	int res=0;
	for (int i=x;i;i-=lowbit(i))
		res+=f[i];
	return res;
}
int main(){
	scanf("%d",&n);
	int x;
	for (int i=1;i<=n;i++){
		scanf("%d",&x);
		x+=i;
		mp[x].push(i);
	}
	int flg=0;
	ll ans=0;
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[i]+=i;
	}
	for (int i=n;i;i--){
		if (!mp[a[i]].empty())
		{
			int num=mp[a[i]].top();
			mp[a[i]].pop();
			add(num+1);
			num-=query(num);
			ans+=i-num;
		}
		else 
		{
			flg=1;
			break;
		}
	}
	if (flg) printf("-1\n");
	else printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>
#define fu(a, b, c) for (ll a = b; a <= c; ++a)
#define fd(a, b, c) for (ll a = b; a >= c; --a)
#define mx(a, b) a = max(a, b)
#define mn(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
const ll N = 2e5 + 1, M = 998244353;
ll t, n, m, k, a, b, c, ans;
ll x[N], y[N];
set<pair<ll, ll>> s;
set<ll> s1;

int Tree[N << 2];
void Update(int l, int r, int x, int rt) {
    if (l == r) {
        Tree[rt]++;
        return;
    }
    int mid = (l + r) >> 1;
    if (x <= mid)
        Update(l, mid, x, rt << 1);
    else
        Update(mid + 1, r, x, rt << 1 | 1);
    Tree[rt] = Tree[rt << 1] + Tree[rt << 1 | 1];
}
int Query(int ql, int qr, int l, int r, int rt) {
    if (l >= ql && r <= qr) {
        return Tree[rt];
    }
    int mid = (l + r) >> 1;
    int ans = 0;
    if (ql <= mid)
        ans += Query(ql, qr, l, mid, rt << 1);
    if (qr > mid)
        ans += Query(ql, qr, mid + 1, r, rt << 1 | 1);
    return ans;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    fu(i, 1, n) cin >> x[i], s.emplace(x[i] + i, i);
    fu(i, 1, n) cin >> y[i];
    fu(i, 1, n) {
        auto j = s.lower_bound({y[i] + i, 0});
        if (j->first ^ y[i] + i) {
            ans = -1;
            break;
        }
        ll k = j->second;
        ans += Query(k, n, 1, n, 1) + k - i;
        Update(1, n, k, 1);
        s.erase(j);
    }
    cout << ans;
}