# ABC150C - Count Order

from itertools import permutations





def main():

    N, *PQ = list(map(int, open(0).read().split()))

    P, Q = PQ[:N], PQ[N:]

    a, b = 0, 0

    for i, perm in enumerate(permutations(list(range(1, N + 1)), N), 1):

        perm = list(perm)

        if perm == P:

            a = i

        if perm == Q:

            b = i

    ans = abs(a - b)

    print(ans)





if __name__ == "__main__":

    main()

# ABC150C - Count Order

from itertools import permutations





def main():

    N = int(eval(input()))

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

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

    perms = list(permutations(list(range(1, N + 1))))

    ans = abs(perms.index(P) - perms.index(Q))

    print(ans)





if __name__ == "__main__":

    main()

# ABC150C - Count Order
from itertools import permutations


def main():
    N, *PQ = list(map(int, open(0).read().split()))
    P, Q = PQ[:N], PQ[N:]
    a, b = 0, 0
    for i, perm in enumerate(permutations(list(range(1, N + 1)), N), 1):
        perm = list(perm)
        if perm == P:
            a = i
        if perm == Q:
            b = i
    ans = abs(a - b)
    print(ans)


if __name__ == "__main__":
    main()

# ABC150C - Count Order
from itertools import permutations


def main():
    N = int(eval(input()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    perms = list(permutations(list(range(1, N + 1))))
    ans = abs(perms.index(P) - perms.index(Q))
    print(ans)


if __name__ == "__main__":
    main()

-    N, *PQ = list(map(int, open(0).read().split())) -    P, Q = PQ[:N], PQ[N:] -    a, b = 0, 0 -    for i, perm in enumerate(permutations(list(range(1, N + 1)), N), 1): -        perm = list(perm) -        if perm == P: -            a = i -        if perm == Q: -            b = i -    ans = abs(a - b) +    N = int(eval(input())) +    P = tuple(map(int, input().split())) +    Q = tuple(map(int, input().split())) +    perms = list(permutations(list(range(1, N + 1)))) +    ans = abs(perms.index(P) - perms.index(Q))