import itertools

N=int(eval(input()))

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

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

list_a = list(range(1,N+1))

tuple_a = tuple(list_a)

ptr = list(itertools.permutations(tuple_a))

ptr_num = len(list(itertools.permutations(tuple_a)))

for i in range(0,ptr_num):

  if ptr[i] == tuple(P):

    P_num=i+1

  if ptr[i] == tuple(Q):

    Q_num=i+1

print((abs(P_num-Q_num)))

import itertools

n=int(eval(input()))

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

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

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

print((abs(jyunretsu.index(p)-jyunretsu.index(q))))
import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
list_a = list(range(1, N + 1))
tuple_a = tuple(list_a)
ptr = list(itertools.permutations(tuple_a))
ptr_num = len(list(itertools.permutations(tuple_a)))
for i in range(0, ptr_num):
    if ptr[i] == tuple(P):
        P_num = i + 1
    if ptr[i] == tuple(Q):
        Q_num = i + 1
print((abs(P_num - Q_num)))

import itertools

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

-N = int(eval(input())) -P = list(map(int, input().split())) -Q = list(map(int, input().split())) -list_a = list(range(1, N + 1)) -tuple_a = tuple(list_a) -ptr = list(itertools.permutations(tuple_a)) -ptr_num = len(list(itertools.permutations(tuple_a))) -for i in range(0, ptr_num): -    if ptr[i] == tuple(P): -        P_num = i + 1 -    if ptr[i] == tuple(Q): -        Q_num = i + 1 -print((abs(P_num - Q_num))) +n = int(eval(input())) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +jyunretsu = list(itertools.permutations(list(range(1, n + 1)))) +print((abs(jyunretsu.index(p) - jyunretsu.index(q))))