import math

n=int(eval(input()))

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

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

a=0

b=0

x=[i+1 for i in range(n)]

y=[i+1 for i in range(n)]



for i in range(n-1):

    a+=x.index(p[i])*math.factorial(n-1-i)

    b+=y.index(q[i])*math.factorial(n-1-i)

    x.remove(p[i])

    y.remove(q[i])





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



n=int(eval(input()))

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

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

x=[i+1 for i in range(n)]



x=list(itertools.permutations(x))

a=0

b=0



for i in range(len(x)):

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

        a=i

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

        b=i



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

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a = 0
b = 0
x = [i + 1 for i in range(n)]
y = [i + 1 for i in range(n)]
for i in range(n - 1):
    a += x.index(p[i]) * math.factorial(n - 1 - i)
    b += y.index(q[i]) * math.factorial(n - 1 - i)
    x.remove(p[i])
    y.remove(q[i])
print((abs(a - b)))

import itertools

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
x = [i + 1 for i in range(n)]
x = list(itertools.permutations(x))
a = 0
b = 0
for i in range(len(x)):
    if list(x[i]) == p:
        a = i
    if list(x[i]) == q:
        b = i
print((abs(a - b)))

-import math +import itertools +x = [i + 1 for i in range(n)] +x = list(itertools.permutations(x)) -x = [i + 1 for i in range(n)] -y = [i + 1 for i in range(n)] -for i in range(n - 1): -    a += x.index(p[i]) * math.factorial(n - 1 - i) -    b += y.index(q[i]) * math.factorial(n - 1 - i) -    x.remove(p[i]) -    y.remove(q[i]) +for i in range(len(x)): +    if list(x[i]) == p: +        a = i +    if list(x[i]) == q: +        b = i