//abc202_c.cpp
//Fri Jun 18 20:08:47 2021

#include <bits/stdc++.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i,n) for (int i=0;i<(n);++i)

using namespace std;
using ll=long long;
typedef pair<int,int> P;

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

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

	ll ans = 0;
	rep(i,n){
//		cout << b[c[i]-1] << endl;
		ans += a[b[c[i]-1]];
	}

	cout << ans << endl;
//	printf("%.4f\n",ans);
}
#include <climits>
#include <iostream>
#include <unordered_map>
#include <map>
#include <vector>
#include <unordered_set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int tt=1;
    while(tt--){
        int n;
        cin>>n;
        map<long long, multiset<long long>> balls;
        for(int i=0;i<n;i++){
            long long x, id;
            cin>>x>>id;
            if(balls.find(id)==balls.end()){
                multiset<long long> locations;
                locations.insert(x);
                balls.insert(make_pair(id, locations));
                continue;
            }
            (balls.find(id)->second).insert(x);
        }
        long long dp[balls.size()][2];
        for(int i=0;i<balls.size();i++){
            for(int j=0;j<2;j++){
                dp[i][j]=0;
            }
        }
        dp[balls.size()-1][0]=abs(*((balls.rbegin()->second).begin()));
        dp[balls.size()-1][1]=abs(*((balls.rbegin()->second).rbegin()));
        int i=balls.size()-2;
        auto it=balls.rbegin();
        it++;
        for(it;it!=balls.rend();it++){
            long long lastmn=*((prev(it)->second).begin());
            long long lastmx=*((prev(it)->second).rbegin());
            long long mn=*((it->second).begin());
            long long mx=*((it->second).rbegin());
            dp[i][0]=min(dp[i+1][1]+abs(mn-lastmn)+lastmx-lastmn, dp[i+1][0]+abs(lastmx-mn)+lastmx-lastmn);
            dp[i][1]=min(dp[i+1][1]+abs(mx-lastmn)+lastmx-lastmn, dp[i+1][0]+abs(lastmx-mx)+lastmx-lastmn);
            i--;
        }
        long long lastmx=*((balls.begin()->second).rbegin());
        long long lastmn=*((balls.begin()->second).begin());
        long long zero=0;
        long long ans=min(dp[0][1]+abs(lastmn)+lastmx-lastmn, dp[0][0]+abs(lastmx)+lastmx-lastmn);
        cout<<ans<<"\n";
    }


}


//3 4 2 1 5 6

//1 2 3
