def main():

    n=int(eval(input()))

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

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

    fact = [1]

    for i in range(1,9):

        fact.append(i*fact[i-1])

    tmp = []

    for i in range(n):

        tmp.append(i+1)

    i=0

    ansp = caldict(fact,i,p,tmp)

    tmp = []

    for i in range(n):

        tmp.append(i+1)

    i=0

    ansq = caldict(fact,i,q,tmp)



    print((abs(ansp-ansq)))

def caldict(f,i,p,tmp):

    if i < len(p)-1:

        nowi = tmp.index(p[i])

        now = tmp.pop(nowi)

        i+=1

        return nowi * f[len(tmp)] + caldict(f,i,p,tmp)

    else:

        return 1





if __name__=='__main__':

    main()

import itertools

def main():

    n=int(eval(input()))

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

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



    perm = list(itertools.permutations(list(range(1,n+1))) )

    ansp = perm.index(tuple(p))

    # qq = list(itertools.permutations(q) )

    ansq = perm.index(tuple(q))



    print((abs(ansp-ansq)))



if __name__=='__main__':

    main()

def main():
    n = int(eval(input()))
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    fact = [1]
    for i in range(1, 9):
        fact.append(i * fact[i - 1])
    tmp = []
    for i in range(n):
        tmp.append(i + 1)
    i = 0
    ansp = caldict(fact, i, p, tmp)
    tmp = []
    for i in range(n):
        tmp.append(i + 1)
    i = 0
    ansq = caldict(fact, i, q, tmp)
    print((abs(ansp - ansq)))


def caldict(f, i, p, tmp):
    if i < len(p) - 1:
        nowi = tmp.index(p[i])
        now = tmp.pop(nowi)
        i += 1
        return nowi * f[len(tmp)] + caldict(f, i, p, tmp)
    else:
        return 1


if __name__ == "__main__":
    main()

import itertools


def main():
    n = int(eval(input()))
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    perm = list(itertools.permutations(list(range(1, n + 1))))
    ansp = perm.index(tuple(p))
    # qq = list(itertools.permutations(q) )
    ansq = perm.index(tuple(q))
    print((abs(ansp - ansq)))


if __name__ == "__main__":
    main()

+import itertools + + -    fact = [1] -    for i in range(1, 9): -        fact.append(i * fact[i - 1]) -    tmp = [] -    for i in range(n): -        tmp.append(i + 1) -    i = 0 -    ansp = caldict(fact, i, p, tmp) -    tmp = [] -    for i in range(n): -        tmp.append(i + 1) -    i = 0 -    ansq = caldict(fact, i, q, tmp) +    perm = list(itertools.permutations(list(range(1, n + 1)))) +    ansp = perm.index(tuple(p)) +    # qq = list(itertools.permutations(q) ) +    ansq = perm.index(tuple(q)) - - -def caldict(f, i, p, tmp): -    if i < len(p) - 1: -        nowi = tmp.index(p[i]) -        now = tmp.pop(nowi) -        i += 1 -        return nowi * f[len(tmp)] + caldict(f, i, p, tmp) -    else: -        return 1