#include<bits/stdc++.h>
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define ppb 			pop_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define M               1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define w(x)            int x; cin>>x; while(x--)
#define ld				long double
#define nline 			"\n"
#define all(x) 			(x).begin(), (x).end()
// memset(matrix, 0, sizeof(matrix));

// for debug
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}


inline int mod(int x) {return (x % M + M) % M;}
inline int add(int a, int b) {return mod(mod(a) + mod(b));}
inline int sub(int a, int b) {return mod(mod(a) - mod(b) + M);}
inline int mul(int a, int b) {return mod(mod(a) * mod(b));}
inline int power(int a, int p) {int r = 1; while (p) { if (p & 1) r = mul(r, a); a = mul(a, a); p >>= 1; } return r;}
inline int modInv(int a) {return power(a, M - 2);}
inline int divi(int a, int b) {return mul(a, modInv(b));}
inline int fact(int a) {int r = 1; while (a) { r = mul(r, a); a--; } return r;}
inline int ncr(int n, int r) {return mul(mul(fact(n), modInv(fact(n - r))), modInv(fact(r)));}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b);}
inline int ceil(int x, int y) {if (x % y == 0) {return x / y;} return x / y + 1;}
void solve()
{
	int n;
	cin >> n;

	vector<int> arr(n);

	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}

	map<int, vector<int> > h;
	for (int i = 0; i < n; i++)
	{
		h[arr[i]].push_back(i);
	}
	int ans = 0;
	for (auto x : h)
	{
		if (x.second.size() >= 2)
		{
			ans += (x.second.size() * (x.second.size() - 1)) / 2;
		}
	}

	int v = (n - 1) * n / 2;

	cout << v - ans << nline;
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("debug.txt", "w", stderr);
#endif

	// w(t)
	// {
	// 	solve();

	// }
	solve();
	return 0;
}
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define m_p make_pair
#define pb push_back
#define ld long double
#define fi first
#define se second
#define ull unsigned long long
#define mod 1000000007
#define md 998244353
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
#define int ll
using namespace std;
inline void read(int &x){
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0'||c>'9'){
		if(c=='-')negative=-1;
		c=getchar();
	}
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll qpow(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
const int N=200010;
int n;
int a[N]; 
int b[N];
int c[N];
int bit[N];
vector<int>v;
queue<int>q[N];
map<int,int>mp;
int lowbit(int x){
	return x&(-x);
}
void add(int pos,int val){
	while(pos<=n){
		bit[pos]+=val;
		pos+=lowbit(pos);
	}
}
int getsum(int pos){
	int res=0;
	while(pos>=1){
		res+=bit[pos];
		pos-=lowbit(pos);
	}
	return res;
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>n;
	forn(i,1,n)cin>>a[i],a[i]+=i;
	forn(i,1,n)cin>>b[i],b[i]+=i;
	forn(i,1,n){
		v.pb(b[i]);
	}
	forn(i,1,n){
		mp[b[i]]=i;
	}
	forn(i,1,n){
		q[mp[b[i]]].push(i);
	}
	forn(i,1,n){
		if(mp[a[i]]<1||q[mp[a[i]]].empty()){
			cout<<-1<<"\n";
			return 0;
		}
		c[i]=q[mp[a[i]]].front();
		q[mp[a[i]]].pop();
	}
	int ans=0;
	forn(i,1,n){
		add(c[i],1);
		ans+=i-getsum(c[i]);
	}
	cout<<ans<<"\n";
	return 0;
}
