#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
#define pri(str) cout << str << endl
using ll = long long;
using P = pair<int, int>;

const ll MX = 1e18;
const long double PI = acos(-1);

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> inline vector<T> divisor(T n) {
    vector<T> ret;
    for (T i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end());
    return ret;
}

template<class T> inline vector<pair<T, T>> prime_factor(T n) {
    vector<pair<T, T>> ret;
    for (T i = 2; i * i <= n; i++) {
        if (n % i != 0) continue;
        T tmp = 0;
        while (n % i == 0) {
            tmp++;
            n /= i;
        }
        ret.push_back(make_pair(i, tmp));
    }
    if (n != 1) ret.push_back(make_pair(n, 1));
    return ret;
}

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

    vector<int> bc(n); rep(i, n) bc[b[c[i]]] += 1;
    ll count = 0;
    rep(i, n)
    {
        count += bc[a[i]];
    }

    pri(count);
    return 0;
}

#include<bits/stdc++.h>
#define int long long
#define uc getchar()
#define ph putchar
#define R register
#define CN const
#define rint R int
#define cint CN int
#define ps ph(' ')
#define pe ph('\n')
using namespace std;
cint inf=1e9+7;
cint mix=2e7+5;
inline int in();
inline void out(rint x);
int n=in(),k=in();
int c[10],nt;
inline int f(rint x){
	nt=0;
	while(x)c[++nt]=x%10,x/=10;
	sort(c+1,c+nt+1);
//cerr<<nt<<endl;
	rint rt=0;
	for(rint i=1;i<=nt;++i)
	(rt*=10)+=c[nt+1-i]-c[i];
	return rt;
}
#undef int
int main() {
#define int long long
	while(k--)n=f(n);
	out(n),pe;
}
inline int in() {
	rint x=0;
	R bool f=0;
	R char s=uc;
	while((s<'0'||'9'<s)&&s!='-')s=uc;
	if(s=='-')f=1,s=uc;
	while('0'<=s&&s<='9')(x*=10)+=s^48,s=uc;
	return f?-x:x;
}
inline void out(rint x) {
	if(x<0)ph('-'),x=-x;
	rint y=x/10;
	if(y)out(y);
	ph((x-y*10)^48);
}