#include <iostream>

using namespace std;

int main()
{
    long int n, count=0, b1=0;
    cin >> n;
    
    long int a[n], b[n], c[n];
    for(long int i=0;i<n;i++) cin >> a[i];
    for(long int i=0;i<n;i++) cin >> b[i];
    
    for(long int i=0;i<n;i++){
        for(long int j=0;j<n;j++){
            for(long int k=0;k<n;k++){
                if(b1<4) {cin >> c[b1]; b1++;}
                if(a[i] == b[j] && j+1 == c[k]) count++;
            }
        }
    }
    
    cout << count;

    return 0;
}