import itertools





n = int(eval(input()))

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

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



l = list(itertools.permutations(p))



l.sort()



p_i = 0

q_i = 0



for i in range(len(l)):

    if list(l[i]) == p:

        p_i = i

    if list(l[i]) == q:

        q_i = i



print((abs(p_i - q_i)))





import itertools





n = int(eval(input()))

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

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



permu_dict = list(itertools.permutations([i for i in range(1, n+1)]))

permu_dict = sorted(permu_dict)



print((abs(permu_dict.index(p)-permu_dict.index(q))))

import itertools

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
l = list(itertools.permutations(p))
l.sort()
p_i = 0
q_i = 0
for i in range(len(l)):
    if list(l[i]) == p:
        p_i = i
    if list(l[i]) == q:
        q_i = i
print((abs(p_i - q_i)))

import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
permu_dict = list(itertools.permutations([i for i in range(1, n + 1)]))
permu_dict = sorted(permu_dict)
print((abs(permu_dict.index(p) - permu_dict.index(q))))

-p = list(map(int, input().split())) -q = list(map(int, input().split())) -l = list(itertools.permutations(p)) -l.sort() -p_i = 0 -q_i = 0 -for i in range(len(l)): -    if list(l[i]) == p: -        p_i = i -    if list(l[i]) == q: -        q_i = i -print((abs(p_i - q_i))) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +permu_dict = list(itertools.permutations([i for i in range(1, n + 1)])) +permu_dict = sorted(permu_dict) +print((abs(permu_dict.index(p) - permu_dict.index(q))))