n=int(eval(input()))

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

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



import itertools



l=list(itertools.permutations(list(range(1,n+1))))

#print(l)

print((abs(l.index(p)-l.index(q))))
#c

import math



n=int(eval(input()))

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

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

orderp=0

orderq=0



for i in range(n):

  orderp+=(p[i]-1)*math.factorial(n-i-1)

  orderq+=(q[i]-1)*math.factorial(n-i-1)

  for j in range(i+1,n):

    if p[j]>p[i]:

      p[j]-=1

    if q[j]>q[i]:

      q[j]-=1

  #print(orderp,orderq)

  #print(p,q)





print((abs(orderp-orderq)))
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
import itertools

l = list(itertools.permutations(list(range(1, n + 1))))
# print(l)
print((abs(l.index(p) - l.index(q))))

# c
import math

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
orderp = 0
orderq = 0
for i in range(n):
    orderp += (p[i] - 1) * math.factorial(n - i - 1)
    orderq += (q[i] - 1) * math.factorial(n - i - 1)
    for j in range(i + 1, n):
        if p[j] > p[i]:
            p[j] -= 1
        if q[j] > q[i]:
            q[j] -= 1
    # print(orderp,orderq)
    # print(p,q)
print((abs(orderp - orderq)))

+# c +import math + -p = tuple(map(int, input().split())) -q = tuple(map(int, input().split())) -import itertools - -l = list(itertools.permutations(list(range(1, n + 1)))) -# print(l) -print((abs(l.index(p) - l.index(q)))) +p = list(map(int, input().split())) +q = list(map(int, input().split())) +orderp = 0 +orderq = 0 +for i in range(n): +    orderp += (p[i] - 1) * math.factorial(n - i - 1) +    orderq += (q[i] - 1) * math.factorial(n - i - 1) +    for j in range(i + 1, n): +        if p[j] > p[i]: +            p[j] -= 1 +        if q[j] > q[i]: +            q[j] -= 1 +    # print(orderp,orderq) +    # print(p,q) +print((abs(orderp - orderq)))