import itertools

N = int(eval(input()))

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

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

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

NLL = sorted(list(itertools.permutations(NL,N)))

a = 0

b = 0

for i in range(len(NLL)):

    if ''.join(list(NLL[i])) == P:

        a = i+1

    if ''.join(list(NLL[i])) == Q:

        b = i+1

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

import math

N = int(eval(input()))

P = ''.join(input().split())

Q = ''.join(input().split())

np = sorted(list([''.join(x) for x in itertools.permutations([str(i+1) for i in range(N)])]))

print((abs((np.index(P)+1)-(np.index(Q)+1))))
import itertools

N = int(eval(input()))
NL = [str(i) for i in range(1, N + 1)]
P = input().replace(" ", "")
Q = input().replace(" ", "")
NLL = sorted(list(itertools.permutations(NL, N)))
a = 0
b = 0
for i in range(len(NLL)):
    if "".join(list(NLL[i])) == P:
        a = i + 1
    if "".join(list(NLL[i])) == Q:
        b = i + 1
print((abs(a - b)))

import itertools
import math

N = int(eval(input()))
P = "".join(input().split())
Q = "".join(input().split())
np = sorted(
    list(["".join(x) for x in itertools.permutations([str(i + 1) for i in range(N)])])
)
print((abs((np.index(P) + 1) - (np.index(Q) + 1))))

+import math -NL = [str(i) for i in range(1, N + 1)] -P = input().replace(" ", "") -Q = input().replace(" ", "") -NLL = sorted(list(itertools.permutations(NL, N))) -a = 0 -b = 0 -for i in range(len(NLL)): -    if "".join(list(NLL[i])) == P: -        a = i + 1 -    if "".join(list(NLL[i])) == Q: -        b = i + 1 -print((abs(a - b))) +P = "".join(input().split()) +Q = "".join(input().split()) +np = sorted( +    list(["".join(x) for x in itertools.permutations([str(i + 1) for i in range(N)])]) +) +print((abs((np.index(P) + 1) - (np.index(Q) + 1))))