import math



def calc_order(A, N):

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

    order = 1

    for i in range(N):

        a = A[i]

        len_l = len(l)

        index = l.index(a)

        # print(a, index, math.factorial(len_l - 1), order)

        order += index * math.factorial(len_l - 1)

        del l[index]

    return order



def main():

    N = int(eval(input()))

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

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

    ord_p = calc_order(P, N)

    ord_q = calc_order(Q, N)

    ans = abs(ord_p - ord_q)

    # print(ord_p, ord_q)

    print(ans)



if __name__ == '__main__':

    main()

def factorial(n):

    ret = 1

    for i in range(n):

        ret *= (i + 1)

    return ret



def calc_order(A, N):

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

    order = 1

    for i in range(N):

        a = A[i]

        len_l = N - i

        index = l.index(a)

        # print(a, index, factorial(len_l - 1), order)

        order += index * factorial(len_l - 1)

        del l[index]

    return order



def main():

    N = int(eval(input()))

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

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

    ord_p = calc_order(P, N)

    ord_q = calc_order(Q, N)

    ans = abs(ord_p - ord_q)

    # print(ord_p, ord_q)

    print(ans)



if __name__ == '__main__':

    main()

import math


def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = len(l)
        index = l.index(a)
        # print(a, index, math.factorial(len_l - 1), order)
        order += index * math.factorial(len_l - 1)
        del l[index]
    return order


def main():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)


if __name__ == "__main__":
    main()

def factorial(n):
    ret = 1
    for i in range(n):
        ret *= i + 1
    return ret


def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        # print(a, index, factorial(len_l - 1), order)
        order += index * factorial(len_l - 1)
        del l[index]
    return order


def main():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)


if __name__ == "__main__":
    main()

-import math +def factorial(n): +    ret = 1 +    for i in range(n): +        ret *= i + 1 +    return ret -        len_l = len(l) +        len_l = N - i -        # print(a, index, math.factorial(len_l - 1), order) -        order += index * math.factorial(len_l - 1) +        # print(a, index, factorial(len_l - 1), order) +        order += index * factorial(len_l - 1)