#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

class BIT{
    public:
    int size;
    vector<long long> tree;
    
    BIT(int n){
        size = n;
        tree = vector<long long>(n, 0);
        return;
    }
    
    void add(int n, long long a) {
        assert(n < size);
        int x = n;
        while (x < size){
            tree[x] += a;
            x |= x+1;
        }
        return;
    }
    
    long long getsum(int n) {
        assert(n < size);
        int x = n+1;
        long long sum = 0;
        while (x > 0) {
            sum += tree[x-1];
            x &= x-1;
        }
        return sum;
    }

};

int main() {
    int N;
    cin >> N;
    vector<ll> A(N), B(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        A[i] += i;
    }
    for (int i = 0; i < N; i++) {
        cin >> B[i];
        B[i] += i;
    }
    {
        vector<ll> C = A, D = B;
        sort(C.begin(), C.end());
        sort(D.begin(), D.end());
        if (C != D) {
            cout << -1 << endl;
            return 0;
        }
    }
    map<ll, ll> mp;
    set<ll> st;
    map<ll, ll> cnt;
    for (int i = 0; i < N; i++) {
        st.insert(A[i]);
        cnt[A[i]]++;
    }
    ll t = 0;
    for (ll n : st) {
        mp[n] = t;
        t += cnt[n];
    }
    map<ll, ll> cnt2;
    for (int i = 0; i < N; i++) {
        ll pre = A[i];
        A[i] = mp[A[i]]+cnt2[A[i]];
        cnt2[pre]++;
    }
    cnt2.clear();
    for (int i = 0; i < N; i++) {
        ll pre = B[i];
        B[i] = mp[B[i]]+cnt2[B[i]];
        cnt2[pre]++;
    }
    {
        vector<ll> tmp(N);
        for (int i = 0; i < N; i++) {
            tmp[B[i]] = i;
        }
        for (int i = 0; i < N; i++) {
            A[i] = tmp[A[i]];
        }
    }
    ll ans = 0;
    BIT bit(N);
    for (ll i = 0; i < N; i++) {
        ans += i - bit.getsum(A[i]);
        bit.add(A[i], 1);
    }
    cout << ans << endl;
    return 0;
}
// code by lynmisakura.wish to be accepted!
#include<bits/stdc++.h>
using namespace std;

#define REP(i,N) for(int i = 0;i < N;i++)
#define rng(i,a,b) for(int i = a;i < b;i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define mp make_pair
#define pb push_back
#define eb emplace_back

#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
    cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
    return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
    cout << "{\n"; for (auto& vec : arr)cout << "  " << vec << ",\n"; cout << "}";
    return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
    cout << "{" << p.first << "," << p.second << "}" << '\n';
    return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
    for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
    cin >> p.first >> p.second; return in;
}
template<class T> void debug_a(T *a , int N){
  cout << "debug array : {";
  for(int i = 0;i < N;i++){
    cout << *(a + i) << (i < N-1 ? ", " : "}\n");
  }
}
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

template<class T> bool chmin(T& a,T b){ if(a > b){ a = b;return true;}else{return false;} }
template<class T> bool chmax(T& a,T b){ if(a < b){ a = b;return true;}else{return false;} }
template<class T> T __ceil(T a,T b){return (a + b - 1) / b;}

int main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);
  //cout << fixed << setprecision(20);
  
  string s;cin >> s;
  string t = "ZONe";

  int ans = 0;
  REP(i,9){
    ans += s.substr(i,4) == t;
  }

  cout << ans << endl;

}
