class UnionFind_AB: def __init__(self, N): self.p = [-1] * N self.L = [i for i in range(N)] self.R = [i for i in range(N)] def reset(self): for i in range(len(self.p)): self.p[i] = -1 self.L[i] = i self.R[i] = i def root(self, x): while self.p[x] >= 0: x = self.p[x] return x def same(self, x, y): return self.root(x) == self.root(y) def unite(self, x, y): x = self.root(x) y = self.root(y) if x == y: return p = self.p if p[x] > p[y]: p[y] += p[x] p[x] = y self.L[y] = min(self.L[y],self.L[x]) self.R[y] = max(self.R[y],self.R[x]) else: p[x] += p[y] p[y] = x self.L[x] = min(self.L[y],self.L[x]) self.R[x] = max(self.R[y],self.R[x]) def size(self, x): return -self.p[self.root(x)] def left(self, v): return self.L[self.root(v)] def right(self,v): return self.R[self.root(v)] class UnionFind_IDX: def __init__(self, N): self.p = [-1] * N self.num_pair = [0] * N self.score = 0 def root(self, x): while self.p[x] >= 0: x = self.p[x] return x def same(self, x, y): return self.root(x) == self.root(y) def unite(self, x, y): x = self.root(x) y = self.root(y) if x == y: return self.score -= self.sub_score(x) + self.sub_score(y) p = self.p if p[x] > p[y]: p[y] += p[x] p[x] = y self.num_pair[y] += self.num_pair[x] self.score += self.sub_score(y) else: p[x] += p[y] p[y] = x self.num_pair[x] += self.num_pair[y] self.score += self.sub_score(x) def add_pair(self,v): self.score -= self.sub_score(self.root(v)) self.num_pair[self.root(v)]+=1 self.score += self.sub_score(self.root(v)) def size(self, x): return -self.p[self.root(x)] def sub_score(self,v): return min(-self.p[self.root(v)],self.num_pair[self.root(v)]) import sys from typing import Union input = sys.stdin.readline def main(): n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) z = list(map(int,input().split())) ida = [-1]*n idb = [-1]*n for i in range(n): a[i]-=1 b[i]-=1 z[i]-=1 ida[a[i]]=i idb[b[i]]=i pos_left = [-1]*n l = [-1]*n r = [n]*n mid = [[]for i in range(n)] ufa = UnionFind_AB(n) ufb = UnionFind_AB(n) for q in range(17): for i in range(n): if r[i]-l[i]>1: mid[(l[i]+r[i])//2].append(i) if q > 0: ufa.reset() ufb.reset() for k in range(n): ia = ida[k] ib = idb[k] if 0