from sys import stdin

import math



n = int(stdin.readline().rstrip())

li = list(map(int,stdin.readline().rstrip().split()))

lin = list(map(int,stdin.readline().rstrip().split()))

from itertools import permutations

lis = permutations(li)



liv = []



for i in lis:

    liv.append(list(i))

liv.sort()

a = liv.index(li)

b = liv.index(lin)



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



n = int(eval(input()))

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

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

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



for i,j in enumerate(permutations(li)):

    if j == p:

        num_p = i

    if j == q:

        num_q = i



print((abs(num_p-num_q)))
from sys import stdin
import math

n = int(stdin.readline().rstrip())
li = list(map(int, stdin.readline().rstrip().split()))
lin = list(map(int, stdin.readline().rstrip().split()))
from itertools import permutations

lis = permutations(li)
liv = []
for i in lis:
    liv.append(list(i))
liv.sort()
a = liv.index(li)
b = liv.index(lin)
print((abs(a - b)))

from itertools import permutations

n = int(eval(input()))
li = [i for i in range(1, n + 1)]
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, j in enumerate(permutations(li)):
    if j == p:
        num_p = i
    if j == q:
        num_q = i
print((abs(num_p - num_q)))

-from sys import stdin -import math - -n = int(stdin.readline().rstrip()) -li = list(map(int, stdin.readline().rstrip().split())) -lin = list(map(int, stdin.readline().rstrip().split())) -lis = permutations(li) -liv = [] -for i in lis: -    liv.append(list(i)) -liv.sort() -a = liv.index(li) -b = liv.index(lin) -print((abs(a - b))) +n = int(eval(input())) +li = [i for i in range(1, n + 1)] +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +for i, j in enumerate(permutations(li)): +    if j == p: +        num_p = i +    if j == q: +        num_q = i +print((abs(num_p - num_q)))