#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>

#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long

using namespace std;

int n, a[200010], b[200010], c[200010];
map<int, vector<int>> m;
map<int, int> id;
ll res;

int bit[200010];

ll sum(int i){
    ll s = 0;
    while(i>0){
        s += bit[i];
        i -= i&(-i);
    }
    return s;
}

void add(int i, int x){
    while(i < 200010){
        bit[i] += x;
        i += (i&(-i));
    }
}


int main(){
    cin >> n;
    rep(i,n){cin>>a[i];a[i]+=i;}
    rep(i,n){cin>>b[i];b[i]+=i;}
    rep(i,n){
        m[b[i]].push_back(i);
    }
    rep(i,n){
        int t = a[i];
        if(m[t].size() <= id[t]){
            cout << -1 << endl;
            return 0;
        }
        else {
            c[i] = m[t][id[t]]+1; id[t]++;
        }
    }

    rep(i,n){
        res += i - sum(c[i]);
        add(c[i], 1);
    }
    cout << res << endl;
}
#include <bits/stdc++.h>
using namespace std;

void DBG() { cerr << endl; }
template<class Head, class... Tail>
void DBG(Head H, Tail... T) { cerr << ' ' << H; DBG(T...); }
#define dbg(...) cerr << "(" << (#__VA_ARGS__) << "):", DBG(__VA_ARGS__)

using ll = long long;
using ld = long double;

#define sqr(a) ll(a) * (a)
#define siz(a) int(a.size())
#define ints(a...) int a; read(a)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define trav(i, v) for (const auto i : v)
#define Fill(a, b) memset(a, b, sizeof(a))
#define rep(i, b, n) for (auto i = b; i < n; i++)
#define per(i, b, n) for (auto i = b; i >= n; i--)
#define unify(a) sort(all(a)), a.resize(unique(all(a)) - a.begin())

template <class T> using vec = vector<T>;
template <class... Args> inline void read(Args&... args) { ((cin >> args), ...); }
template <class... Args> inline void show(Args... args) { ((cout << args << " "), ...); }
template <class T1, class T2> inline bool ckmin(T1 &a, T2 b) { return a > b ? a = b, 1 : 0; }
template <class T1, class T2> inline bool ckmax(T1 &a, T2 b) { return a < b ? a = b, 1 : 0;  }
template <class T> inline void operator>> (istream& in, vector<T>& v) { rep(i, 0, siz(v)) in >> v[i]; }
template <class T> inline void operator<< (ostream& out, const vector<T>& v) { rep(i, 0, siz(v)) out << v[i] << " \n"[i + 1 == siz(v)]; }

const int MOD = 1e9 + 7;

void solve() {
    ints(n);
    string s; read(s);
    auto idx = s.find("fox");
    while (idx != string::npos) {
        s.erase(idx, 3);
        idx = s.find("fox", max<int>(idx - 2, 0));
    }
    cout << siz(s);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(12);
    int _ = 1;
    // cin >> _;
    rep(i, 1, _ + 1) {
        // printf("Case %d: ", i);
        // Solution solve;
        solve();
    }
    return 0;
}