#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 500500;
int t[maxn];
int n;
void upd(int p,int x) {
	for(; p < maxn; p |= (p + 1)) t[p] += x;
}
int get(int p) {
	int r = 0;
	for(; p >= 0; p = (p & (p + 1)) - 1) r += t[p];
	return r;
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	vector<pair<int, int>> a(n), b(n);		
	for(int i = 0; i < n; i++) {
		cin >> a[i].first;
		a[i].first += i;
		a[i].second = i;
	}
	for(int i = 0; i < n; i++) {
		cin >> b[i].first;
		b[i].first += i;
		b[i].second = i;
	}
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	vector<int> p(n);
	for(int i = 0; i < n; i++) {
		if(a[i].first != b[i].first) {
			cout << -1;
			return 0;
		}
		p[a[i].second] = b[i].second;
	}
	ll ans = 0;
	for(int i = n - 1; i >= 0; i--) {
		ans += get(p[i]);
		upd(p[i], 1);
	}
	cout << ans;
}
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
#include <complex>

using namespace std;

#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define INF 1001001001001001011
#define ld(val) printf("%s : %lld\n", #val, val);
#define sd(val) printf("%s : %s\n", #val, val.c_str());

typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, P> PP;
typedef tuple<ll, ll, ll> TP;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  vector<ll> c(n);
 
  rep(i, n) {
    cin >> a[i];
  }
  rep(i, n) {
    cin >> b[i];
  }
  rep(i, n) {
    cin >> c[i];
    c[i]--;
  }
  map<ll, ll> mp;
  rep(i, n) {
    mp[b[c[i]]]++;
  }

  ll ans = 0;
  rep(i, n) {
    ans += mp[a[i]];
  }
  cout << ans << endl;
  return 0;
}