import sys

input = sys.stdin.readline





def main():

    N = int(eval(input()))

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

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

    ps = ""

    for i in P:

        ps = ps + str(i)

    qs = ""

    for i in Q:

        qs = qs + str(i)

    import itertools

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

    i = 0

    p = q = 0

    for a in itertools.permutations(A):

        s = ""

        for j in a:

            s = s + str(j)

        if ps == s:

            p = i

        if qs == s:

            q = i

        i += 1

    print((abs(p-q)))





if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline





def main():

    N = int(eval(input()))

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

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



    import itertools

    a = list(itertools.permutations(list(range(1, N+1))))

    print((abs(a.index(P) - a.index(Q))))





if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ps = ""
    for i in P:
        ps = ps + str(i)
    qs = ""
    for i in Q:
        qs = qs + str(i)
    import itertools

    A = [i + 1 for i in range(N)]
    i = 0
    p = q = 0
    for a in itertools.permutations(A):
        s = ""
        for j in a:
            s = s + str(j)
        if ps == s:
            p = i
        if qs == s:
            q = i
        i += 1
    print((abs(p - q)))


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    import itertools

    a = list(itertools.permutations(list(range(1, N + 1))))
    print((abs(a.index(P) - a.index(Q))))


if __name__ == "__main__":
    main()

-    P = list(map(int, input().split())) -    Q = list(map(int, input().split())) -    ps = "" -    for i in P: -        ps = ps + str(i) -    qs = "" -    for i in Q: -        qs = qs + str(i) +    P = tuple(map(int, input().split())) +    Q = tuple(map(int, input().split())) -    A = [i + 1 for i in range(N)] -    i = 0 -    p = q = 0 -    for a in itertools.permutations(A): -        s = "" -        for j in a: -            s = s + str(j) -        if ps == s: -            p = i -        if qs == s: -            q = i -        i += 1 -    print((abs(p - q))) +    a = list(itertools.permutations(list(range(1, N + 1)))) +    print((abs(a.index(P) - a.index(Q))))