import itertools



N = int(eval(input()))

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

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



L = [i for i in range(1, N+1)]

a = 0

b = 0



#順列作成

L_list = list(itertools.permutations(L))



for i in range(len(L_list)):

  if list(L_list[i]) == P:

    a = i+1

  if list(L_list[i]) == Q:

    b = i+1



print((abs(a-b)))
#パクリ

import itertools



N = int(eval(input()))

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

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



lis=list(itertools.permutations(P))

lis.sort()



#indexで要素の何番目か取得

print((abs(lis.index(P)-lis.index(Q))))

import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = [i for i in range(1, N + 1)]
a = 0
b = 0
# 順列作成
L_list = list(itertools.permutations(L))
for i in range(len(L_list)):
    if list(L_list[i]) == P:
        a = i + 1
    if list(L_list[i]) == Q:
        b = i + 1
print((abs(a - b)))

# パクリ
import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
lis = list(itertools.permutations(P))
lis.sort()
# indexで要素の何番目か取得
print((abs(lis.index(P) - lis.index(Q))))

+# パクリ -P = list(map(int, input().split())) -Q = list(map(int, input().split())) -L = [i for i in range(1, N + 1)] -a = 0 -b = 0 -# 順列作成 -L_list = list(itertools.permutations(L)) -for i in range(len(L_list)): -    if list(L_list[i]) == P: -        a = i + 1 -    if list(L_list[i]) == Q: -        b = i + 1 -print((abs(a - b))) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +lis = list(itertools.permutations(P)) +lis.sort() +# indexで要素の何番目か取得 +print((abs(lis.index(P) - lis.index(Q))))