n = int(eval(input()))

tp = tuple(map(int, input().split()))

tq = tuple(map(int, input().split()))



import itertools as it



l = list(it.permutations(list(range(1, n+1))))



print((abs(l.index(tp) - l.index(tq))))
f=lambda:tuple(map(int,input().split()))

n=int(eval(input()))

tp,tq=f(),f()

import itertools as it

l=list(it.permutations(list(range(1,n+1))))

print((abs(l.index(tp)-l.index(tq))))
n = int(eval(input()))
tp = tuple(map(int, input().split()))
tq = tuple(map(int, input().split()))
import itertools as it

l = list(it.permutations(list(range(1, n + 1))))
print((abs(l.index(tp) - l.index(tq))))

f = lambda: tuple(map(int, input().split()))
n = int(eval(input()))
tp, tq = f(), f()
import itertools as it

l = list(it.permutations(list(range(1, n + 1))))
print((abs(l.index(tp) - l.index(tq))))

+f = lambda: tuple(map(int, input().split())) -tp = tuple(map(int, input().split())) -tq = tuple(map(int, input().split())) +tp, tq = f(), f()