import itertools

N = int(eval(input()))

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

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



tem_list = []

for i in range(1,N+1):

    tem_list.append(i)



check_list = list(itertools.permutations(tem_list,N))



for i in range(0,len(check_list)):

    if check_list[i] == P_list:

        tem_1 = i

for j in range(0,len(check_list)):

    if check_list[j] == Q_list:

        tem_2 = j



print((abs(tem_2-tem_1)))
import itertools

N = int(eval(input()))

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

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



tem_list = []

for i in range(1,N+1):

    tem_list.append(i)



check_list = list(itertools.permutations(tem_list,N))



for i in range(0,len(check_list)):

    if check_list[i] == P_list:

        tem_1 = i

    if check_list[i] == Q_list:

        tem_2 = i



print((abs(tem_2-tem_1)))
import itertools

N = int(eval(input()))
P_list = tuple(map(int, input().split()))
Q_list = tuple(map(int, input().split()))
tem_list = []
for i in range(1, N + 1):
    tem_list.append(i)
check_list = list(itertools.permutations(tem_list, N))
for i in range(0, len(check_list)):
    if check_list[i] == P_list:
        tem_1 = i
for j in range(0, len(check_list)):
    if check_list[j] == Q_list:
        tem_2 = j
print((abs(tem_2 - tem_1)))

import itertools

N = int(eval(input()))
P_list = tuple(map(int, input().split()))
Q_list = tuple(map(int, input().split()))
tem_list = []
for i in range(1, N + 1):
    tem_list.append(i)
check_list = list(itertools.permutations(tem_list, N))
for i in range(0, len(check_list)):
    if check_list[i] == P_list:
        tem_1 = i
    if check_list[i] == Q_list:
        tem_2 = i
print((abs(tem_2 - tem_1)))

-for j in range(0, len(check_list)): -    if check_list[j] == Q_list: -        tem_2 = j +    if check_list[i] == Q_list: +        tem_2 = i