#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

template <typename T>
class BIT {
   public:
    BIT(int n) : N(n), A(n + 1, 0) {}

    T sum(int i) {
        T res = 0;
        while (i > 0) {
            res += A[i];
            i -= i & (-i);
        }
        return res;
    }

    T sum(int i, int j) {
        return sum(j) - sum(i - 1);
    }

    void add(int i, T x) {
        while (i <= N) {
            A[i] += x;
            i += (i & (-i));
        }
    }

   private:
    int N;
    vector<T> A;
};

int main() {
    int N; cin >> N;
    vector<int> A(N), B(N);
    REP(i, 0, N) cin >> A[i];
    REP(i, 0, N) cin >> B[i];

    map<int, set<int>> ml, mr;
    REP(i, 0, N) mr[A[i] + i].insert(i);

    BIT<int> bit(N);

    ll ans = 0;
    REP(i, 0, N) {
        // cout << "# i : " << i << ", B[i] : " << B[i] << endl;
        int idx = -1;
        if (ml.count(B[i] + i) && !ml[B[i] + i].empty()) {
            auto itr = ml[B[i] + i].begin();
            idx = *itr;
            ml[B[i] + i].erase(itr);
        } else if (mr.count(B[i] + i) && !mr[B[i] + i].empty()) {
            auto itr = mr[B[i] + i].begin();
            idx = *itr;
            mr[B[i] + i].erase(itr);
        }

        if (idx == -1) {
            cout << -1 << endl;
            return 0;
        }
        // cout << "--> idx : " << idx << endl;

        if (mr[A[i] + i].count(i)) {
            mr[A[i] + i].erase(i);
            ml[A[i] + i].insert(i);
        }

        ans += bit.sum(idx + 1, N);
        bit.add(idx + 1, 1);
    }
    cout << ans << endl;

    return 0;
}

#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T, class ...U> void kout (T v, U ...e) { cerr << v << ' ', kout(e...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 400010, inf = 1e9 + 7;

int n, v[MAX_N];

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1;i <= n + n;++i)
		cin >> v[i];

	priority_queue<int, vector<int>, greater<int>> pq;

	ll res = 0;

	for (int l = n, r = n+1;l >= 1;--l, ++r) {
		res += max(v[l], v[r]);

		int pqv = pq.size() ? pq.top() : inf;

		if (pqv < min(v[l], v[r])) {
			pq.pop();
			res -= pqv - min(v[l], v[r]);
			pq.push(min(v[l], v[r]));
		}

		pq.push(max(v[l], v[r]));
	}

	cout << res << '\n';
}

