n = int(eval(input()))

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

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



import itertools

plist = list(itertools.permutations([i+1 for i in range(n)]))



count = 1



resultp = 0

resultq = 0



for pp in plist:

    if(list(pp) == p):

        resultp = count

    if (list(pp) == q):

        resultq = count

    count += 1



print((abs(resultp - resultq)))
import math





# 数列が何番目かを返却するメソッド

def junban(N, str):

    int_list = str.split()  # 入力文字を配列化

    int_list = [int(i) for i in int_list]

    used_list = []

    num = 0

    for element in int_list:

        N -= 1

        used_count = 0

        for used in used_list:

            if element > used:

                used_count += 1

        num += (element - 1 - used_count) * math.factorial(N)

        used_list.append(element)

    # print(num)

    return num





# 大きさNを受け取る

N = int(eval(input()))

# 数列Pを受け取る

P = eval(input())

# 数列Qを受け取る

Q = eval(input())

# それぞれが何番目か求める

p_int = junban(N, P)

q_int = junban(N, Q)

print((abs(p_int - q_int)))

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
import itertools

plist = list(itertools.permutations([i + 1 for i in range(n)]))
count = 1
resultp = 0
resultq = 0
for pp in plist:
    if list(pp) == p:
        resultp = count
    if list(pp) == q:
        resultq = count
    count += 1
print((abs(resultp - resultq)))

import math

# 数列が何番目かを返却するメソッド
def junban(N, str):
    int_list = str.split()  # 入力文字を配列化
    int_list = [int(i) for i in int_list]
    used_list = []
    num = 0
    for element in int_list:
        N -= 1
        used_count = 0
        for used in used_list:
            if element > used:
                used_count += 1
        num += (element - 1 - used_count) * math.factorial(N)
        used_list.append(element)
    # print(num)
    return num


# 大きさNを受け取る
N = int(eval(input()))
# 数列Pを受け取る
P = eval(input())
# 数列Qを受け取る
Q = eval(input())
# それぞれが何番目か求める
p_int = junban(N, P)
q_int = junban(N, Q)
print((abs(p_int - q_int)))

-n = int(eval(input())) -p = list(map(int, input().split())) -q = list(map(int, input().split())) -import itertools +import math -plist = list(itertools.permutations([i + 1 for i in range(n)])) -count = 1 -resultp = 0 -resultq = 0 -for pp in plist: -    if list(pp) == p: -        resultp = count -    if list(pp) == q: -        resultq = count -    count += 1 -print((abs(resultp - resultq))) +# 数列が何番目かを返却するメソッド +def junban(N, str): +    int_list = str.split()  # 入力文字を配列化 +    int_list = [int(i) for i in int_list] +    used_list = [] +    num = 0 +    for element in int_list: +        N -= 1 +        used_count = 0 +        for used in used_list: +            if element > used: +                used_count += 1 +        num += (element - 1 - used_count) * math.factorial(N) +        used_list.append(element) +    # print(num) +    return num + + +# 大きさNを受け取る +N = int(eval(input())) +# 数列Pを受け取る +P = eval(input()) +# 数列Qを受け取る +Q = eval(input()) +# それぞれが何番目か求める +p_int = junban(N, P) +q_int = junban(N, Q) +print((abs(p_int - q_int)))