from itertools import permutations

from collections import defaultdict



N = int(eval(input()))



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

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

res = 0

loop = 0



d = defaultdict(int)



for perm in permutations(list(range(1,N+1))):

    d[perm] = loop

    loop += 1



res = abs(d[P] - d[Q])



print(res)
from itertools import *

N = int(eval(input()))

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

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

dic = dict()

i = 0

for a in permutations(list(range(1,N+1)),N):

    dic[a] = i

    i += 1

print((abs(dic[P]-dic[Q])))
from itertools import permutations
from collections import defaultdict

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
res = 0
loop = 0
d = defaultdict(int)
for perm in permutations(list(range(1, N + 1))):
    d[perm] = loop
    loop += 1
res = abs(d[P] - d[Q])
print(res)

from itertools import *

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
dic = dict()
i = 0
for a in permutations(list(range(1, N + 1)), N):
    dic[a] = i
    i += 1
print((abs(dic[P] - dic[Q])))

-from itertools import permutations -from collections import defaultdict +from itertools import * -res = 0 -loop = 0 -d = defaultdict(int) -for perm in permutations(list(range(1, N + 1))): -    d[perm] = loop -    loop += 1 -res = abs(d[P] - d[Q]) -print(res) +dic = dict() +i = 0 +for a in permutations(list(range(1, N + 1)), N): +    dic[a] = i +    i += 1 +print((abs(dic[P] - dic[Q])))