from itertools import permutations

n = int(eval(input()))

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

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



nums = list(permutations(list(range(1,n+1)), n))

a = 0

b = 0

c = 0

for i in nums:

  c += 1

  if i == p:

    a = c

  if i == q:

    b = c

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

n = int(eval(input()))

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

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



nums = list(permutations(list(range(1,n+1)),n))

print((abs(nums.index(p) - nums.index(q))))
from itertools import permutations

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
nums = list(permutations(list(range(1, n + 1)), n))
a = 0
b = 0
c = 0
for i in nums:
    c += 1
    if i == p:
        a = c
    if i == q:
        b = c
print((abs(a - b)))

from itertools import permutations

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
nums = list(permutations(list(range(1, n + 1)), n))
print((abs(nums.index(p) - nums.index(q))))

-a = 0 -b = 0 -c = 0 -for i in nums: -    c += 1 -    if i == p: -        a = c -    if i == q: -        b = c -print((abs(a - b))) +print((abs(nums.index(p) - nums.index(q))))