import itertools

n=int(eval(input()))

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

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

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

cnt1=0

cnt2=0

for i in range(len(arrs)):

  if arrs[i]==p:

    cnt1=i

  if arrs[i]==q:

    cnt2=i

print((abs(cnt2-cnt1)))
def fact(n):

  ret=1

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

    ret=ret*i

  return ret



n=int(eval(input()))

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

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

cnt1=0

cnt2=0

checked1=[0]*(n+1)

checked2=[0]*(n+1)

for i in range(n):

  checked1[arr1[i]]=1

  checked2[arr2[i]]=1

  cnt1+=(arr1[i]-1-sum(checked1[:arr1[i]]))*fact(n-i-1)

  cnt2+=(arr2[i]-1-sum(checked2[:arr2[i]]))*fact(n-i-1)

print((abs(cnt1-cnt2)))
import itertools

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

def fact(n):
    ret = 1
    for i in range(2, n + 1):
        ret = ret * i
    return ret


n = int(eval(input()))
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
cnt1 = 0
cnt2 = 0
checked1 = [0] * (n + 1)
checked2 = [0] * (n + 1)
for i in range(n):
    checked1[arr1[i]] = 1
    checked2[arr2[i]] = 1
    cnt1 += (arr1[i] - 1 - sum(checked1[: arr1[i]])) * fact(n - i - 1)
    cnt2 += (arr2[i] - 1 - sum(checked2[: arr2[i]])) * fact(n - i - 1)
print((abs(cnt1 - cnt2)))

-import itertools +def fact(n): +    ret = 1 +    for i in range(2, n + 1): +        ret = ret * i +    return ret + -p = tuple(map(int, input().split())) -q = tuple(map(int, input().split())) -arrs = list(itertools.permutations(list(range(1, n + 1)))) +arr1 = list(map(int, input().split())) +arr2 = list(map(int, input().split())) -for i in range(len(arrs)): -    if arrs[i] == p: -        cnt1 = i -    if arrs[i] == q: -        cnt2 = i -print((abs(cnt2 - cnt1))) +checked1 = [0] * (n + 1) +checked2 = [0] * (n + 1) +for i in range(n): +    checked1[arr1[i]] = 1 +    checked2[arr2[i]] = 1 +    cnt1 += (arr1[i] - 1 - sum(checked1[: arr1[i]])) * fact(n - i - 1) +    cnt2 += (arr2[i] - 1 - sum(checked2[: arr2[i]])) * fact(n - i - 1) +print((abs(cnt1 - cnt2)))