n = int(eval(input()))

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

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



from collections import deque

tuples=[]

def f(que):

    if len(que)==n:

        tuples.append(tuple(que))

        return

    

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

        if v not in que:

            que.append(v)

            f(que)

            que.pop()

f(deque([]))

#print(tuples)

#print(tuples.index(p))

#print(tuples.index(q))

print((abs(tuples.index(p)-tuples.index(q))))
import itertools

N = int(eval(input()))

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

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



a,b = 0,0

for i, perm in enumerate(itertools.permutations(list(range(1, N+1))), 1):

    if P==perm:

        a = i

    if Q==perm:

        b = i

    if a > 0 and b > 0:

        print((abs(a-b)))

        exit()
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
from collections import deque

tuples = []


def f(que):
    if len(que) == n:
        tuples.append(tuple(que))
        return
    for v in range(1, n + 1):
        if v not in que:
            que.append(v)
            f(que)
            que.pop()


f(deque([]))
# print(tuples)
# print(tuples.index(p))
# print(tuples.index(q))
print((abs(tuples.index(p) - tuples.index(q))))

import itertools

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

-n = int(eval(input())) -p = tuple(map(int, input().split())) -q = tuple(map(int, input().split())) -from collections import deque +import itertools -tuples = [] - - -def f(que): -    if len(que) == n: -        tuples.append(tuple(que)) -        return -    for v in range(1, n + 1): -        if v not in que: -            que.append(v) -            f(que) -            que.pop() - - -f(deque([])) -# print(tuples) -# print(tuples.index(p)) -# print(tuples.index(q)) -print((abs(tuples.index(p) - tuples.index(q)))) +N = int(eval(input())) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +a, b = 0, 0 +for i, perm in enumerate(itertools.permutations(list(range(1, N + 1))), 1): +    if P == perm: +        a = i +    if Q == perm: +        b = i +    if a > 0 and b > 0: +        print((abs(a - b))) +        exit()