import itertools



n = int(input().strip())

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

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

p_cnt = 0

q_cnt = 0

for i, e in enumerate(sorted(list(itertools.permutations(p)))):

    if p == e:

        p_cnt = i

    if q == e:

        q_cnt = i

print((abs(p_cnt - q_cnt)))

from itertools import permutations as perms



n = int(input().strip())

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

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

lis = [(i + 1) for i in range(n)]

a = -1

b = -1

cnt = 0

for elem in perms(lis, n):

    cnt += 1

    if elem == p:

        a = cnt

    if elem == q:

        b = cnt

    if a != -1 and b != -1:

        break

print((abs(a - b)))

import itertools

n = int(input().strip())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
p_cnt = 0
q_cnt = 0
for i, e in enumerate(sorted(list(itertools.permutations(p)))):
    if p == e:
        p_cnt = i
    if q == e:
        q_cnt = i
print((abs(p_cnt - q_cnt)))

from itertools import permutations as perms

n = int(input().strip())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
lis = [(i + 1) for i in range(n)]
a = -1
b = -1
cnt = 0
for elem in perms(lis, n):
    cnt += 1
    if elem == p:
        a = cnt
    if elem == q:
        b = cnt
    if a != -1 and b != -1:
        break
print((abs(a - b)))

-import itertools +from itertools import permutations as perms -p_cnt = 0 -q_cnt = 0 -for i, e in enumerate(sorted(list(itertools.permutations(p)))): -    if p == e: -        p_cnt = i -    if q == e: -        q_cnt = i -print((abs(p_cnt - q_cnt))) +lis = [(i + 1) for i in range(n)] +a = -1 +b = -1 +cnt = 0 +for elem in perms(lis, n): +    cnt += 1 +    if elem == p: +        a = cnt +    if elem == q: +        b = cnt +    if a != -1 and b != -1: +        break +print((abs(a - b)))