
//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>


#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<LL,LL>
#define micro 0.000001

using namespace std;

LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }

#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif

pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,0},{1,-1}, {-1,1} , {-1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };

const int max_v = 2e5 + 7;
const int max_k = 5e2 + 7;
const int bsz = (1ll << 10) + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 998244353;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; };

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);	    
	int n; ci(n);

	vector<LL> va(n);
	vector<LL> vb(n);

	fa(i, 0, n) ci(va[i]);
	fa(i, 0, n) ci(vb[i]);
	

	unordered_map<LL, vector<int>> xh;
	fd(i, n-1, -1) va[i] += i,xh[va[i]].push_back(i);

	LL d = 0;
	LL ans = 0;
	vector<LL> arr(n+1);

	auto upd = [&](int i)
	{
		++i;
		for (; i<=n; i += i & -i) ++arr[i];
	};

	auto qur = [&](int i)
	{
		++i;
		LL ret = 0;
		for (; i; i -= i & -i)ret += arr[i];
		return ret;
	};

	fa(i, 0, n)
	{
		if (xh[vb[i] + d].empty())
		{
			co(-1);
			return 0;
		}

		int t = xh[vb[i] + d].back();

		

		xh[vb[i] + d].pop_back();

		ans += t - qur(t);
		upd(t);
		++d;
	}

	co(ans);

	return 0;
}



#include "bits/stdc++.h"
#define int long long
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 998244353;
template <typename T>
struct BIT {
	int n;          // 配列の要素数(数列の要素数+1)
	vector<T> bit;  // データの格納先
	BIT(int n_) : n(n_ + 1), bit(n, 0) {}
	void add(int i, T x) {
		for (int idx = i; idx < n; idx += (idx & -idx)) {
			bit[idx] += x;
		}
	}
	T sum(int i) {
		T s(0);
		for (int idx = i; idx > 0; idx -= (idx & -idx)) {
			s += bit[idx];
		}
		return s;
	}
};
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N; cin >> N;
	vector<int>A(N), B(N);
	for (int i = 0; i < N; i++)cin >> A[i];
	for (int i = 0; i < N; i++)cin >> B[i];
	map<int, vector<int>>mp;
	for (int i = N - 1; i >= 0; i--)mp[i + A[i]].push_back(i);
	vector<int>c(N);
	BIT<int>bit(N + 100);
	for (int i = 0; i < N; i++) {
		if (mp[i + B[i]].empty()) {
			cout << -1 << endl; return 0;
		}
		int idx = mp[i + B[i]].back();
		mp[i + B[i]].pop_back();
		c[i] = idx + 1;
	}
	int ans = 0;
	for (int i = 0; i < N; i++) {

		ans += i - bit.sum(c[i]);
		bit.add(c[i], 1);
	}
	cout << ans << endl;
	return 0;

}

