#include #include #include #include using namespace std; #define FOR(i, to) for(int i = 0 ; i < to ; ++i) typedef long long LL; LL T[1000000]; int K; void init(){ FOR(i,800000) T[i] = 0; } void inc(int ww){ int w = K+ww-1; while(w > 0){ T[w]++; w/=2; } } LL get(int w, int odi, int poi, int pokial){ if (pokial <= 0) return 0; if (pokial >= poi) return T[w]; int delim = (odi+poi)/2; LL vratim = 0; if (pokial > delim) vratim += get(w*2+1, delim+1,poi, pokial); if (pokial >= odi) vratim += get(2*w, odi, delim, pokial); return vratim; } int N; int in[3][160000]; int IP[3][160000]; int main(){ while(true){ init(); LL res = 0; scanf("%d ",&N); if (N == 0) break; K = 1; while(K < N) K*=2; FOR(i,3){ FOR(j,N){ scanf("%d ",&in[i][j]); IP[i][ in[i][j] ] = j; } } FOR(i,N){ int cislo = in[0][i]; res += get( 1, 1, K, IP[1][cislo]); inc( IP[2][cislo] + 1); } printf("%lld\n",res); } return 0; }