#include<bits/stdc++.h>
// #include<iostream>
// #include<algorithm>
// #include<vector>
// #include<set>
// #include<queue>
// #include<map>
using namespace std;


#define ll long long
#define ld long double

#define For(n) for(ll i=0;i<n;i++)
#define Forr(a,b) for(ll i=a;i<b;i++)

#define sortt(x) sort(x.begin(),x.end())
#define rsortt(x) sort(x.rbegin(),x.rend())
#define condsortt(x) sort(x.begin(),x.end(),sortBy)

#define MOD 1000000007
#define en "\n"


#define F first
#define S second


#define input(x,n) for(ll i=0;i<n;i++) cin>>x[i]
#define v(A,n) vector<ll> A(n)
#define vi(x) vector<ll> x
#define pb(x) push_back(x)
#define MP make_pair
#define PI 3.14159265


void print(vector<ll> vec)
{
    for(auto itr:vec)
        cout<<itr<<" ";
    cout<<en;
}


void solve()
{
    ll n;
    cin>>n;
    v(A,n);
    input(A,n);
    vector<ll> vec[n+1];
    For(n)
    {
        ll a;cin>>a;
        vec[a].pb(i);
    }
    v(mp,n);
    For(n)
    {
        ll a;cin>>a;
        mp[a-1]++;
    }
    ll ans = 0;
    map<ll,ll> kk;
    For(n)
    {
        ll ch = A[i];
        if(kk.find(ch)!=kk.end())
        {
            ans+=kk[ch];
            continue;
        }
        int zz=0;
        for(auto j:vec[ch])
        {
            zz+=mp[j];
        }
        ans+=zz;
        kk[ch]=zz;
    }
    cout<<ans<<en;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t=1;
    // cin>>t;
    for(ll i=1;i<=t;i++)
    {
        // cout<<"Case #"<<i<<": ";
        solve();
    }
    return 0;
}
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define pdd pair<double,double>
#define pii pair<ll,ll>
#define fi first
#define sec second
#define MAXN 1000000000000000005
#define N 300005
#define lgn 20


using namespace std;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << std::endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif



int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif
    //Type Your Code Here

   	ll n;
    pair<ll,pii> apx[100000]; 
    cin>>n;
    for(ll i=0;i<n;i++) {
        ll a,p,x;
        cin>>a>>p>>x;
        apx[i] = {p,{a,x}};
    }
    sort(apx,apx+n);
    ll ans = -1;
    for(ll i=0;i<n;i++) {
        ll a,p,x;
        a = apx[i].sec.fi;
        p = apx[i].fi;
        x = apx[i].sec.sec;
        if(a<x) {
            ans = p;
            break;
        }
    }
    cout<<ans;

    return 0;
}