#include <set>
#include <cstdio>
using namespace std;
#define maxn 200005
#define int long long
set < pair < int, int > > st;
set < pair < int, int > > :: iterator it;
int n;
int t[maxn];

int lowbit( int x ) {
	return x & ( -x );
}

void add( int x, int val ) {
	for( int i = x;i <= n;i += lowbit( i ) ) t[i] += val;
}

int query( int x ) {
	int ans = 0;
	for( int i = x;i;i -= lowbit( i ) ) ans += t[i];
	return ans;
}

signed main() {
	scanf( "%lld", &n );
	for( int i = 1, x;i <= n;i ++ ) {
		scanf( "%lld", &x );
		st.insert( make_pair( x + i, i ) );
	}
	bool flag = 0; int ans = 0;
	for( int i = 1, x;i <= n;i ++ ) {
		scanf( "%lld", &x );
		it = st.lower_bound( make_pair( x + i, 0 ) );
		if( it == st.end() ) flag = 1;
		else {
			int v = query( ( *it ).second );
			add( 1, 1 ), add( ( *it ).second, -1 );
			ans += ( *it ).second + v - i;
			st.erase( it );
		}
	}
	if( flag ) printf( "-1\n" );
	else printf( "%lld\n", ans );
	return 0;
}
#include <bits/stdc++.h>
typedef long long i64;
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using namespace std;

const int xn = 2e5;
int n, a[xn], b[xn], p[xn], c[xn];
map<int, queue<int>> mp;

void ap(int i) {for (; i < n; i |= i + 1) {++c[i];}}
int sp(int i) {int x = 0;
 for (; i; i &= i - 1) {x += c[i - 1];} return x;}

int main() {
  // freopen("#.in", "r", stdin); // @
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  cin >> n;
  rep(i, 0, n) {cin >> a[i]; a[i] += i; mp[a[i]].push(i);}
  bool ok = true;
  rep(i, 0, n) {
    cin >> b[i]; b[i] += i;
    queue<int> &q = mp[b[i]];
    if (!sz(q)) {ok = false;}
    else {p[i] = q.front(); q.pop();}
  }

  if (ok) {
    i64 res = 0;
    rep(i, 0, n) {res += i - sp(p[i]); ap(p[i]);}
    cout << res << '\n';
  } else {cout << "-1\n";}
  return 0;
}

/* 📌https://www.cnblogs.com/George1123/p/tips.html */
