n = int(eval(input()))

P = list(map(int,input().split()))

Q = list(map(int,input().split()))



import itertools



A = []

for i in range(1,n+1):

    A.append(i)

    

p = itertools.permutations(A,n)

p = list(p)



for i in range(len(p)):

    p[i] = list(p[i])





print((abs(p.index(P)-p.index(Q))))
n = int(eval(input()))

P = list(map(int,input().split()))

Q = list(map(int,input().split()))



import itertools

p = [list(i) for i in itertools.permutations(list(range(1,n+1)),n)]



print((abs(p.index(P)-p.index(Q))))
n = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
import itertools

A = []
for i in range(1, n + 1):
    A.append(i)
p = itertools.permutations(A, n)
p = list(p)
for i in range(len(p)):
    p[i] = list(p[i])
print((abs(p.index(P) - p.index(Q))))

n = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
import itertools

p = [list(i) for i in itertools.permutations(list(range(1, n + 1)), n)]
print((abs(p.index(P) - p.index(Q))))

-A = [] -for i in range(1, n + 1): -    A.append(i) -p = itertools.permutations(A, n) -p = list(p) -for i in range(len(p)): -    p[i] = list(p[i]) +p = [list(i) for i in itertools.permutations(list(range(1, n + 1)), n)]