#!/usr/bin/env python3

import sys

import itertools

import bisect





def solve(N: int, P: "List[int]", Q: "List[int]"):



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

    perm = list(itertools.permutations(nums, N))

    perm.sort()



    p_at = 0

    for i, pr in enumerate(perm):

        if pr == tuple(P):

            p_at = i + 1

        if pr == tuple(Q):

            q_at = i + 1

    print((abs(p_at - q_at)))

    return





# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word



    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    solve(N, P, Q)





if __name__ == "__main__":

    main()

#!/usr/bin/env python3

import sys

import itertools

import bisect





def solve(N: int, P: "List[int]", Q: "List[int]"):



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

    perm = list(itertools.permutations(nums, N))

    print((abs(perm.index(tuple(P)) - perm.index(tuple(Q)))))

    return





# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word



    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    solve(N, P, Q)





if __name__ == "__main__":

    main()

#!/usr/bin/env python3
import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):
    nums = [i for i in range(1, N + 1)]
    perm = list(itertools.permutations(nums, N))
    perm.sort()
    p_at = 0
    for i, pr in enumerate(perm):
        if pr == tuple(P):
            p_at = i + 1
        if pr == tuple(Q):
            q_at = i + 1
    print((abs(p_at - q_at)))
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):
    nums = [i for i in range(1, N + 1)]
    perm = list(itertools.permutations(nums, N))
    print((abs(perm.index(tuple(P)) - perm.index(tuple(Q)))))
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)


if __name__ == "__main__":
    main()

-    perm.sort() -    p_at = 0 -    for i, pr in enumerate(perm): -        if pr == tuple(P): -            p_at = i + 1 -        if pr == tuple(Q): -            q_at = i + 1 -    print((abs(p_at - q_at))) +    print((abs(perm.index(tuple(P)) - perm.index(tuple(Q)))))