#ifdef MYDEBUG
#include "pch.h"
#else
#include <bits/stdc++.h>
#define debug(...) 
#endif
using namespace std;

#define int long long
#define endl '\n'

#define sz(x) (int)((x).size())
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()

typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;

template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& a) { return in >> a.first >> a.second; }
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) { return out << a.first << " " << a.second;}

template<typename T>void print(T t) { cout << t; }
template<typename T, typename... Args>void print(T t, Args... args) { cout << t << " "; print(args...); }
template<typename... Args>void printl(Args... args) { print(args...); cout << endl; }
template<typename T>void printa(T& a, bool ln = 0) { for (auto& e : a) print(e, ln?"\n":""); if (!ln) cout << endl; }

#define printr(...) { printl(__VA_ARGS__); return; }
#define inputa(a) for (auto& e : a) cin >> e

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int RAND(int lo = 0, int hi = LLONG_MAX) { return uniform_int_distribution<int>(lo, hi)(rng); }

void google() { static int __gtest__ = 1; cout << "Case #" << __gtest__++ << ": "; }

const double pi = 3.141592653589793;

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cout << setprecision(15) << fixed;
	int t = 1;
	//cin >> t;
	void code();
	while (t--) code();
}

const int mod = 1e9 + 7;
const int mod1 = 998244353;
const int maxn = 1e6 + 1;

void  code()
{
	int n;
	cin >> n;

	int sum = 0;
	for (size_t i = 1; i < maxn; i++)
	{
		sum += i;
		if (sum >= n)
			printr(i);
	}
}
#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
using ll = long long;

const int N = 2e5+5;
int bit[N];
int a[N], b[N], c[N];
pair<int,int> pa[N], pb[N];

void add(int p, int v)
{
    while(p < N)
    {
        bit[p] += v;
        p += p&-p;
    }
}

int ask(int p)
{
    int ret = 0;
    while(p)
    {
        ret += bit[p];
        p -= p&-p;
    }
    return ret;
}


int main()
{
    int n;
    cin >> n;

    for(int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        a[i] += i;
        pa[i] = {a[i], i};
    }    

    for(int i = 1; i <= n; ++i)
    {
        cin >> b[i];
        b[i] += i;
        pb[i] = {b[i], i};
    }

    sort(pa + 1, pa + n + 1);
    sort(pb + 1, pb + n + 1);
    for(int i = 1; i <= n; ++i)
    {
        if(pa[i].first != pb[i].first)
        {
            cout << -1 << endl;
            return 0;
        }
        c[pa[i].second] = pb[i].second;
    }

    ll res = 0;
    for(int i = 1; i <= n; ++i)
    {
        res += i - ask(c[i]) - 1;
        add(c[i], 1);
    }
    cout << res << endl;
    return 0;
}