N = int(eval(input()))

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

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



import itertools

X = list(itertools.permutations(list(range(1,N+1)),N))

for i in range(len(X)):

  x = X[i]

  if P == x:

    a = i 

  if Q == x:

    b = i



print((abs(a-b)))
N = int(eval(input()))

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

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



import itertools

X = list(itertools.permutations(list(range(1,N+1)),N))



print((abs(X.index(Q)-X.index(P))))
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
import itertools

X = list(itertools.permutations(list(range(1, N + 1)), N))
for i in range(len(X)):
    x = X[i]
    if P == x:
        a = i
    if Q == x:
        b = i
print((abs(a - b)))

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
import itertools

X = list(itertools.permutations(list(range(1, N + 1)), N))
print((abs(X.index(Q) - X.index(P))))

-for i in range(len(X)): -    x = X[i] -    if P == x: -        a = i -    if Q == x: -        b = i -print((abs(a - b))) +print((abs(X.index(Q) - X.index(P))))