#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;

bool isUpper(char c)
{
    return 'A' <= c && c <= 'Z';
}

int main()
{
    string s;
    cin >> s;
    int ans = 0;
    rep(i, s.size() - 3)
    {
        if (s[i + 0] == 'Z' && s[i + 1] == 'O' && s[i + 2] == 'N' && s[i + 3] == 'e')
            ans++;
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)

struct BIT {
private:
  int N;

public:
  vector<ll> bit;
  BIT(ll size) {
    N = size;
    bit.resize(N);
  }

  void add(ll a, ll w) {
    for (ll x = a; x <= N; x += x & -x)
      bit[x] += w;
  }

  ll sum(ll a) {
    ll ret = 0;
    for (ll x = a; x > 0; x -= x & -x)
      ret += bit[x];
    return ret;
  }
};

int main() {
  ll N;
  cin >> N;
  vector<ll> li(N, 0);
  vector<ll> comp(N, 0);
  map<pair<ll, ll>, ll> to_idx_b;
  map<ll, ll> cnta, cntb;
  rep(i, 0, N) {
    ll num;
    cin >> num;
    li[i] = num + i;
  }
  rep(i, 0, N) {
    ll num;
    cin >> num;
    comp[i] = num + i;
    to_idx_b[{num + i, cntb[num + i]++}] = i;
  }
  vector<ll> sw(N);
  for (ll i = 0; i < N; i++) {
    ll target = li[i];
    pair<ll, ll> tmp = {target, cnta[target]++};
    if (to_idx_b.count(tmp) == 0) {
      cout << -1 << endl;
      return 0;
    } else {
      sw[i] = to_idx_b[tmp];
    }
  }

  ll swaps = 0;
  BIT bi(N);
  rep(i, 0, N) {
    ll num = sw[i];
    bi.add(num + 1, 1);
    swaps += i - bi.sum(num);
  }

  cout << swaps << endl;
}
