import itertools

n=int(eval(input()))

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

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

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

a=b=-1

for i in range(len(s)):

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

    a=i

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

    b=i

  if a>=0 and b>=0:

    break

print((abs(a-b)))
from bisect import bisect_left

from itertools import permutations

n=int(eval(input()))

p=''.join(map(str,input().split()))

q=''.join(map(str,input().split()))

l=list(range(1,n+1))

A=list(permutations(l,n))

A=[''.join(map(str,i)) for i in A]

A.sort()

a=bisect_left(A,p)

b=bisect_left(A,q)

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

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

from bisect import bisect_left
from itertools import permutations

n = int(eval(input()))
p = "".join(map(str, input().split()))
q = "".join(map(str, input().split()))
l = list(range(1, n + 1))
A = list(permutations(l, n))
A = ["".join(map(str, i)) for i in A]
A.sort()
a = bisect_left(A, p)
b = bisect_left(A, q)
print((abs(a - b)))

-import itertools +from bisect import bisect_left +from itertools import permutations -p = list(map(int, input().split())) -q = list(map(int, input().split())) -s = list(itertools.permutations(list(range(1, n + 1)))) -a = b = -1 -for i in range(len(s)): -    if p == list(s[i]): -        a = i -    if q == list(s[i]): -        b = i -    if a >= 0 and b >= 0: -        break +p = "".join(map(str, input().split())) +q = "".join(map(str, input().split())) +l = list(range(1, n + 1)) +A = list(permutations(l, n)) +A = ["".join(map(str, i)) for i in A] +A.sort() +a = bisect_left(A, p) +b = bisect_left(A, q)