import itertools



n = int(eval(input()))

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

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



list_ = list(range(1, n + 1))



for num ,v in enumerate(itertools.permutations(list_, n)):

    if p == list(v):

        res_p = num + 1

        

for num ,v in enumerate(itertools.permutations(list_, n)):

    if q == list(v):

        res_q = num + 1



ans = abs(res_p - res_q)



print(ans)
import itertools



n = int(eval(input()))

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

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



list_ = list(range(1, n + 1))



for num ,v in enumerate(itertools.permutations(list_, n)):

    if p == list(v):

        res_p = num + 1

        break

for num ,v in enumerate(itertools.permutations(list_, n)):

    if q == list(v):

        res_q = num + 1

        break

ans = abs(res_p - res_q)



print(ans)
import itertools

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
list_ = list(range(1, n + 1))
for num, v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
for num, v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1
ans = abs(res_p - res_q)
print(ans)

import itertools

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
list_ = list(range(1, n + 1))
for num, v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        break
for num, v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1
        break
ans = abs(res_p - res_q)
print(ans)

+        break +        break