from itertools import permutations

N=int(eval(input()))

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

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



for i,p in enumerate(permutations(list(range(1,N+1)))):

    if p==P:

        a=i

    if p==Q:

        b=i

print((abs(a-b)))
from itertools import permutations



N = int(eval(input()))

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

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



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



a = 0

b = 0



for i, name in enumerate(list(permutations(ls, N))):

    name = list(name)

    if name == P:

        a = i

    if name == Q:

        b = i



print((abs(a-b)))

from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
for i, p in enumerate(permutations(list(range(1, N + 1)))):
    if p == P:
        a = i
    if p == Q:
        b = i
print((abs(a - b)))

from itertools import permutations

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
ls = [i for i in range(1, N + 1)]
a = 0
b = 0
for i, name in enumerate(list(permutations(ls, N))):
    name = list(name)
    if name == P:
        a = i
    if name == Q:
        b = i
print((abs(a - b)))

-P = tuple(map(int, input().split())) -Q = tuple(map(int, input().split())) -for i, p in enumerate(permutations(list(range(1, N + 1)))): -    if p == P: +P = list(map(int, input().split())) +Q = list(map(int, input().split())) +ls = [i for i in range(1, N + 1)] +a = 0 +b = 0 +for i, name in enumerate(list(permutations(ls, N))): +    name = list(name) +    if name == P: -    if p == Q: +    if name == Q: