import itertools



if __name__ == '__main__':

    # P and Q are both rearrangements of (1, 2, ..., N)).

    # N! possible permutations of size N

    # among them, let P and Q be the a-th and the b-th

    n = int(eval(input()))

    p = input().split(' ')

    q = input().split(' ')



    for i in range(0, len(q)):

        p[i] = int(p[i])

        q[i] = int(q[i])



    realnums = p.copy()



    realnums.sort()



    perm = list(itertools.permutations(realnums))



    it = 0



    qplace = 0

    pplace = 0



    for i in perm:

        if list(i) == q:

            qplace = it

        if list(i) == p:

            pplace = it

        it += 1

    print((abs(qplace - pplace)))
import itertools



if __name__ == '__main__':

    # P and Q are both rearrangements of (1, 2, ..., N)).

    # N! possible permutations of size N

    # among them, let P and Q be the a-th and the b-th

    n = int(eval(input()))

    p = list(map(int, input().split(' ')))

    q = list(map(int, input().split(' ')))



    realnums = p.copy()



    realnums.sort()



    perm = list(map(list, itertools.permutations(realnums)))



    print((abs(perm.index(q) - perm.index(p))))
import itertools

if __name__ == "__main__":
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(eval(input()))
    p = input().split(" ")
    q = input().split(" ")
    for i in range(0, len(q)):
        p[i] = int(p[i])
        q[i] = int(q[i])
    realnums = p.copy()
    realnums.sort()
    perm = list(itertools.permutations(realnums))
    it = 0
    qplace = 0
    pplace = 0
    for i in perm:
        if list(i) == q:
            qplace = it
        if list(i) == p:
            pplace = it
        it += 1
    print((abs(qplace - pplace)))

import itertools

if __name__ == "__main__":
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(eval(input()))
    p = list(map(int, input().split(" ")))
    q = list(map(int, input().split(" ")))
    realnums = p.copy()
    realnums.sort()
    perm = list(map(list, itertools.permutations(realnums)))
    print((abs(perm.index(q) - perm.index(p))))

-    p = input().split(" ") -    q = input().split(" ") -    for i in range(0, len(q)): -        p[i] = int(p[i]) -        q[i] = int(q[i]) +    p = list(map(int, input().split(" "))) +    q = list(map(int, input().split(" "))) -    perm = list(itertools.permutations(realnums)) -    it = 0 -    qplace = 0 -    pplace = 0 -    for i in perm: -        if list(i) == q: -            qplace = it -        if list(i) == p: -            pplace = it -        it += 1 -    print((abs(qplace - pplace))) +    perm = list(map(list, itertools.permutations(realnums))) +    print((abs(perm.index(q) - perm.index(p))))