import itertools

N=int(eval(input()))

P,Q=[tuple(map(int,input().split())) for i in range(2)]

dictionary=sorted(itertools.permutations(P,N))

print((abs(dictionary.index(P)-dictionary.index(Q))))
import itertools

n=int(eval(input()))

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

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

count=0

a=0

b=0



for i in itertools.permutations(list(range(1,n+1))):

  count+=1

  if i == p:

    a=count

  if i==q:

    b=count

print((abs((count-a)-(count-b))))







import itertools

N = int(eval(input()))
P, Q = [tuple(map(int, input().split())) for i in range(2)]
dictionary = sorted(itertools.permutations(P, N))
print((abs(dictionary.index(P) - dictionary.index(Q))))

import itertools

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

-N = int(eval(input())) -P, Q = [tuple(map(int, input().split())) for i in range(2)] -dictionary = sorted(itertools.permutations(P, N)) -print((abs(dictionary.index(P) - dictionary.index(Q)))) +n = int(eval(input())) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +count = 0 +a = 0 +b = 0 +for i in itertools.permutations(list(range(1, n + 1))): +    count += 1 +    if i == p: +        a = count +    if i == q: +        b = count +print((abs((count - a) - (count - b))))