n = int(eval(input()))

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

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



A = list(range(1, n+1))



from itertools import permutations



li=[]

for i in permutations(A, n):

    li.extend([list(i)])

    

for i, v in enumerate(li):

    if li[i] == p:

        s = i

    if li[i] == q:

        t = i

print((abs(s-t)))
from itertools import permutations



N = int(eval(input()))

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

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



R = [i for i in range(1, N+1)]



li = []

for i in permutations(R, N):

    li.append(list(i))



print((abs(li.index(P) - li.index(Q))))
n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
A = list(range(1, n + 1))
from itertools import permutations

li = []
for i in permutations(A, n):
    li.extend([list(i)])
for i, v in enumerate(li):
    if li[i] == p:
        s = i
    if li[i] == q:
        t = i
print((abs(s - t)))

from itertools import permutations

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = [i for i in range(1, N + 1)]
li = []
for i in permutations(R, N):
    li.append(list(i))
print((abs(li.index(P) - li.index(Q))))

-n = int(eval(input())) -p = list(map(int, input().split())) -q = list(map(int, input().split())) -A = list(range(1, n + 1)) +N = int(eval(input())) +P = list(map(int, input().split())) +Q = list(map(int, input().split())) +R = [i for i in range(1, N + 1)] -for i in permutations(A, n): -    li.extend([list(i)]) -for i, v in enumerate(li): -    if li[i] == p: -        s = i -    if li[i] == q: -        t = i -print((abs(s - t))) +for i in permutations(R, N): +    li.append(list(i)) +print((abs(li.index(P) - li.index(Q))))