from itertools import permutations

N = int(eval(input()))

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

a = b = x = 0

for d in permutations(''.join(map(str, list(range(1, N+1)))), N):

  x += 1

  s = ''.join(d)

  if s == P:

    a = x

  if s == Q:

    b = x

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

N = int(eval(input()))

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

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

i = a = b = 0

for x in permutations(list(range(1, N+1)), N):

  i += 1

  if x == P:

    a = i

  if x == Q:

    b = i

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

N = int(eval(input()))
P, Q = input().replace(" ", ""), input().replace(" ", "")
a = b = x = 0
for d in permutations("".join(map(str, list(range(1, N + 1)))), N):
    x += 1
    s = "".join(d)
    if s == P:
        a = x
    if s == Q:
        b = x
print((abs(a - b)))

from itertools import permutations

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

-P, Q = input().replace(" ", ""), input().replace(" ", "") -a = b = x = 0 -for d in permutations("".join(map(str, list(range(1, N + 1)))), N): -    x += 1 -    s = "".join(d) -    if s == P: -        a = x -    if s == Q: -        b = x +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +i = a = b = 0 +for x in permutations(list(range(1, N + 1)), N): +    i += 1 +    if x == P: +        a = i +    if x == Q: +        b = i