#include <bits/stdc++.h>

using namespace std;

#define MS(a, b) memset(a, b, sizeof(a))
#define REP(a, b, c) for (register int a = b, _n = c; a <= _n; ++a)
#define DREP(a, b, c) for (register int a = b, _n = c; a >= _n; --a)
#define FOR(a, b, c) for (register int a = b, _n = c; a < _n; ++a)
#define EREP(a, b) for (register int a = head[b]; a; a = edge[a].nxt)
#define ll long long

inline int rd () {
    int x = 0; char c = getchar(); bool f = 0;
    while (c < '0' && c != '-') c = getchar();
    if (c == '-') f = 1, c = getchar();
    while (c >= '0') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return f ? -x : x;
}

const int SIZE = 2e5 + 100;

int n;
int a[SIZE], b[SIZE];
int x[SIZE], y[SIZE];

struct cmp1 {
    bool operator () (const int& x1, const int& x2) const {
        if (a[x1] == a[x2]) return x1 < x2;
        return a[x1] < a[x2];
    }
};
struct cmp2 {
    bool operator () (const int& x1, const int& x2) const {
        if (b[x1] == b[x2]) return x1 < x2;
        return b[x1] < b[x2];
    }
};

int sum[SIZE];
void Add (int x) {
    while (x <= n) {
        ++sum[x];
        x += x & -x;
    }
}
int Sum (int x) {
    int res = 0;
    while (x) {
        res += sum[x];
        x -= x & -x;
    }
    return res;
}

void _main () {
    n = rd();
    REP (i, 1, n) a[i] = rd() + i, x[i] = i;
    REP (i, 1, n) b[i] = rd() + i, y[i] = i;
    sort(x + 1, x + n + 1, cmp1());
    sort(y + 1, y + n + 1, cmp2());

    bool flag = 1;
    ll ans = 0;
    REP (i, 1, n) {
        if (a[x[i]] != b[y[i]]) flag = 0;
        a[x[i]] = y[i];
    }
    DREP (i, n, 1) {
        ans += Sum(a[i]);
        Add(a[i]);
    }
    if (flag) printf ("%lld\n", ans);
    else puts("-1");
}

int main () {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif
    _main();
    return 0;
}

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
    vector<T> dat;
    ll sz;
    //all 1-indexed
    BIT(ll sz) : sz(sz){
        dat.assign(++sz, 0);
    }

    T sum(ll k){
        T ret = 0;
        for(++k; k > 0; k -= k & -k) ret += dat[k];
        return (ret);
    }

    void add(ll k, T x){
        for(++k; k < dat.size(); k += k & -k) dat[k] += x;
    }
    
    ll get(T k){
        if(k <= 0) return 0; 
        ll ret = 0;
        int n = 1; while(n < sz) n *= 2;
        for(int i=n/2; i>0; i/=2){
            if(ret+i < sz && dat[ret+i] < k){
                k -= dat[ret+i];
                ret += i;
            }
        }
        return ret;
    }
};

int main(){
    int n; cin >> n;
    map<int,vector<int>> mp;
    vl a(n);
    rep(i,n){
        int b; cin >> b;
        b += i;
        mp[b].push_back(i);
    }
    rep(i,n){
        cin >> a[i];
        a[i] += i;
    }
    for(auto &x : mp) reverse(all(x.second));
    ll ans = 0;
    BIT<ll> bit(n+1);
    //8 6 6 10 8 10
    //10 6 8 10 8 6
    rep(i,n){
        if(mp[a[i]].empty()){
            ans = -1;
            break;
        }
        ll ba = mp[a[i]].back();
        mp[a[i]].pop_back();
        ans += max(0LL, ba-i + bit.sum(n) - bit.sum(ba));
        //debug(ba,ans,a[i],i,bit.sum(n)-bit.sum(i));
        bit.add(ba,1);
    }
    cout << ans << endl;
    //4 6 8 6 5
    //5 8 6 6 4
    //5 4 6 8 6
    //5 8 4 6 6
    //5 8 6 4 6
    //5 8 6 6 4
}