import itertools



N = int(eval(input()))

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

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



LUT = list(itertools.permutations(list(range(1, N + 1))))



Pi, Qi = -1, -1

for i in range(len(LUT)):

    if LUT[i] == P:

        Pi = i

    if LUT[i] == Q:

        Qi = i



print((abs(Qi - Pi)))

from itertools import *



N = int(eval(input()))

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

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



LUT = list(permutations(list(range(1, N + 1)))).index

print((abs(LUT(P) - LUT(Q))))

import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
LUT = list(itertools.permutations(list(range(1, N + 1))))
Pi, Qi = -1, -1
for i in range(len(LUT)):
    if LUT[i] == P:
        Pi = i
    if LUT[i] == Q:
        Qi = i
print((abs(Qi - Pi)))

from itertools import *

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
LUT = list(permutations(list(range(1, N + 1)))).index
print((abs(LUT(P) - LUT(Q))))

-import itertools +from itertools import * -LUT = list(itertools.permutations(list(range(1, N + 1)))) -Pi, Qi = -1, -1 -for i in range(len(LUT)): -    if LUT[i] == P: -        Pi = i -    if LUT[i] == Q: -        Qi = i -print((abs(Qi - Pi))) +LUT = list(permutations(list(range(1, N + 1)))).index +print((abs(LUT(P) - LUT(Q))))