import itertools

N = int(eval(input()))

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

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

seq = [x for x in range(1,N+1)]

L = list(itertools.permutations(seq))

for i in range(len(L)):

    X = list(L[i])

    if X == P:

        a = i + 1

    if X == Q:

        b = i + 1

ans = abs(a-b)

print(ans)
import itertools



N = int(eval(input()))

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

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



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

ans = abs(A.index(P) - A.index(Q))

print(ans)
import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
seq = [x for x in range(1, N + 1)]
L = list(itertools.permutations(seq))
for i in range(len(L)):
    X = list(L[i])
    if X == P:
        a = i + 1
    if X == Q:
        b = i + 1
ans = abs(a - b)
print(ans)

import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
A = list(itertools.permutations(list(range(1, N + 1))))
ans = abs(A.index(P) - A.index(Q))
print(ans)

-P = list(map(int, input().split())) -Q = list(map(int, input().split())) -seq = [x for x in range(1, N + 1)] -L = list(itertools.permutations(seq)) -for i in range(len(L)): -    X = list(L[i]) -    if X == P: -        a = i + 1 -    if X == Q: -        b = i + 1 -ans = abs(a - b) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +A = list(itertools.permutations(list(range(1, N + 1)))) +ans = abs(A.index(P) - A.index(Q))