def abc150_c_count_order():

    N = int(eval(input()))

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

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



    def solve(items, current):

        if items == []:

            permutations.append(current)

            return

        for item in items:

            next = current + [item]

            new_items = [i for i in items if i != item]

            solve(new_items, next)



    permutations = []

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

    current = []

    solve(items, current)



    a = permutations.index(P)

    b = permutations.index(Q)

    print((abs(a - b)))





##########

if __name__ == "__main__":

    abc150_c_count_order()

def abc150_c_count_order_2():

    N = int(eval(input()))

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

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

    factorial = [1]

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

        factorial.append(factorial[i-1] * i)



    def find_index(permutation, lst):

        indices = []

        for p in permutation:

            i = lst.index(p)

            indices.append(i)

            lst.pop(i)

        # print(indices)

        idx = 0

        for i in range(N):

            idx += factorial[N - i - 1] * (indices[i])

        # print(idx)

        return idx



    a = find_index(P, list(range(1, N+1)))

    b = find_index(Q, list(range(1, N+1)))

    print((abs(a-b)))





##########

if __name__ == "__main__":

    abc150_c_count_order_2()

def abc150_c_count_order():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))

    def solve(items, current):
        if items == []:
            permutations.append(current)
            return
        for item in items:
            next = current + [item]
            new_items = [i for i in items if i != item]
            solve(new_items, next)

    permutations = []
    items = list(range(1, N + 1))
    current = []
    solve(items, current)
    a = permutations.index(P)
    b = permutations.index(Q)
    print((abs(a - b)))


##########
if __name__ == "__main__":
    abc150_c_count_order()

def abc150_c_count_order_2():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    factorial = [1]
    for i in range(1, N + 1):
        factorial.append(factorial[i - 1] * i)

    def find_index(permutation, lst):
        indices = []
        for p in permutation:
            i = lst.index(p)
            indices.append(i)
            lst.pop(i)
        # print(indices)
        idx = 0
        for i in range(N):
            idx += factorial[N - i - 1] * (indices[i])
        # print(idx)
        return idx

    a = find_index(P, list(range(1, N + 1)))
    b = find_index(Q, list(range(1, N + 1)))
    print((abs(a - b)))


##########
if __name__ == "__main__":
    abc150_c_count_order_2()

-def abc150_c_count_order(): +def abc150_c_count_order_2(): +    factorial = [1] +    for i in range(1, N + 1): +        factorial.append(factorial[i - 1] * i) -    def solve(items, current): -        if items == []: -            permutations.append(current) -            return -        for item in items: -            next = current + [item] -            new_items = [i for i in items if i != item] -            solve(new_items, next) +    def find_index(permutation, lst): +        indices = [] +        for p in permutation: +            i = lst.index(p) +            indices.append(i) +            lst.pop(i) +        # print(indices) +        idx = 0 +        for i in range(N): +            idx += factorial[N - i - 1] * (indices[i]) +        # print(idx) +        return idx -    permutations = [] -    items = list(range(1, N + 1)) -    current = [] -    solve(items, current) -    a = permutations.index(P) -    b = permutations.index(Q) +    a = find_index(P, list(range(1, N + 1))) +    b = find_index(Q, list(range(1, N + 1))) -    abc150_c_count_order() +    abc150_c_count_order_2()