import itertools

n = int(eval(input()))

p = input().replace(' ', '')

q = input().replace(' ', '')



for i, num in enumerate(itertools.permutations(list(range(1,n+1)))):

    num = ''.join(map(str, list(num)))

    if num == p:

        break

for j, num in enumerate(itertools.permutations(list(range(1,n+1)))):

    num = ''.join(map(str, list(num)))

    if num == q:

        break



print((abs(i-j)))
import itertools

n = int(eval(input()))

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

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



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





def f(val):

    for i, x in enumerate(N):

        if x == val:

            return i+1



print((abs(f(p) - f(q))))

import itertools

n = int(eval(input()))
p = input().replace(" ", "")
q = input().replace(" ", "")
for i, num in enumerate(itertools.permutations(list(range(1, n + 1)))):
    num = "".join(map(str, list(num)))
    if num == p:
        break
for j, num in enumerate(itertools.permutations(list(range(1, n + 1)))):
    num = "".join(map(str, list(num)))
    if num == q:
        break
print((abs(i - j)))

import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
N = list(itertools.permutations(list(range(1, n + 1))))


def f(val):
    for i, x in enumerate(N):
        if x == val:
            return i + 1


print((abs(f(p) - f(q))))

-p = input().replace(" ", "") -q = input().replace(" ", "") -for i, num in enumerate(itertools.permutations(list(range(1, n + 1)))): -    num = "".join(map(str, list(num))) -    if num == p: -        break -for j, num in enumerate(itertools.permutations(list(range(1, n + 1)))): -    num = "".join(map(str, list(num))) -    if num == q: -        break -print((abs(i - j))) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +N = list(itertools.permutations(list(range(1, n + 1)))) + + +def f(val): +    for i, x in enumerate(N): +        if x == val: +            return i + 1 + + +print((abs(f(p) - f(q))))