import itertools

N = int(eval(input()))

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

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

n_list = [i for i in range(1,N+1)]

n_perm = sorted(list(itertools.permutations(n_list, N)))

a = 0

b = 0



for i in range(len(n_perm)):

    if list(n_perm[i]) == P:

        a = i

        break



for k in range(len(n_perm)):

    if list(n_perm[k]) == Q:

        b = k

        break

        

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

n=int(eval(input()))

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

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

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

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

import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
n_list = [i for i in range(1, N + 1)]
n_perm = sorted(list(itertools.permutations(n_list, N)))
a = 0
b = 0
for i in range(len(n_perm)):
    if list(n_perm[i]) == P:
        a = i
        break
for k in range(len(n_perm)):
    if list(n_perm[k]) == Q:
        b = k
        break
print((abs(a - b)))

import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
l = sorted(list(itertools.permutations(list(range(1, n + 1)))))
print((abs(l.index(p) - l.index(q))))

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