#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ull unsigned long long
#define ln cout<<"\n"
#define all(x) x.begin(),x.end()

typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int,int> pi;
typedef pair<long long,long long> pl;
#define fs(i,a,n) for (int i = a; i <=b; i++)
#define fd(i,a,n) for (int i = a; i <=b; i--)
#define mod    1000000007




void solve(){
int a,b,c;
int n;
cin>>n;
vector<pair<pi,int>> v(n);

for (int i = 0; i < n; i++)
{
    cin>>a>>b>>c;
    v[i].s=a;
    v[i].f.f=b;
    v[i].f.s=c;
}

ll ans=0;
for (int i = 0; i < n; i++)
{
    for (int j = i+1; j < n; j++)
    {
        if(v[i].f.f<=v[j].f.f && v[i].f.s>v[j].f.s){
            ans++;continue;
        }
        if(v[j].f.f<=v[i].f.f && v[j].f.s>v[i].f.s){
            ans++;continue;
        } 
        if(v[j].f.f<=v[i].f.f && v[i].f.f<v[j].f.s){
            ans++;continue;
        } 
        if(v[i].f.f<=v[j].f.f && v[j].f.f<v[i].f.s){
            ans++;continue;
        } 
        if(v[i].f.s==v[j].f.f){
            if((v[i].s==3 || v[i].s==1)&&(v[j].s==2 || v[j].s==1))ans++;
            continue;
        }  
                if(v[j].f.s==v[i].f.f){
            if((v[j].s==3 || v[j].s==1)&&(v[i].s==2 || v[i].s==1))ans++;
            continue;
        }  
    }
    
}
cout<<ans;



}


int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n=1;
// cin>>n;
while(n--){
    solve();
}

}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 100005
#define maxm 200005
#define INF 1234567890
#define eps 1e-15
#define p 1000000007
const double pi=acos(-1);
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
template<class T,class ...S>inline bool read(T &x,S &...y){return read(x)&&read(y...);}
ll n,x,a[maxn],b[maxn],c[maxn],ans;
vector<ll>v[maxn];
int main()
{
    read(n);
    for(ll i=1;i<=n;i++)
    {
        read(x);
        a[x]++;
    }
    for(ll i=1;i<=n;i++)
    {
        read(b[i]);
        v[b[i]].push_back(i);
    }
    for(ll i=1;i<=n;i++)
    {
        read(x);
        c[x]++;
    }
    for(ll i=1;i<=n;i++)
    {
        for(ll j=0;j<v[i].size();j++)
        {
            ans+=a[i]*c[v[i][j]];
        }
    }
    print(ans,'\n');
    return 0;
}
