import itertools



n = int(eval(input()))

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

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

num_list = []

num = 0

a = 0

b = 0



for i in itertools.permutations(list(range(1,n+1))):

    num_list = list(i)

    num += 1

    if num_list == p_list:

        a = num

    if num_list == q_list:

        b = num



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



n = int(eval(input()))

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

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



num_list = list(itertools.permutations(list(range(1,n+1))))



print((abs(num_list.index(p)-num_list.index(q))))
import itertools

n = int(eval(input()))
p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
num_list = []
num = 0
a = 0
b = 0
for i in itertools.permutations(list(range(1, n + 1))):
    num_list = list(i)
    num += 1
    if num_list == p_list:
        a = num
    if num_list == q_list:
        b = num
print((abs(a - b)))

import itertools

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

-p_list = list(map(int, input().split())) -q_list = list(map(int, input().split())) -num_list = [] -num = 0 -a = 0 -b = 0 -for i in itertools.permutations(list(range(1, n + 1))): -    num_list = list(i) -    num += 1 -    if num_list == p_list: -        a = num -    if num_list == q_list: -        b = num -print((abs(a - b))) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +num_list = list(itertools.permutations(list(range(1, n + 1)))) +print((abs(num_list.index(p) - num_list.index(q))))