import itertools



N=int(eval(input()))

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

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

a=0

b=0



index=0

for i in itertools.permutations(list(range(1,N+1)), N):   

    if P==list(i):

        a=index

    if Q==list(i):

        b=index

    index+=1

print((abs(b-a)))
import sys

input = sys.stdin.readline



def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))



def main():

    mod=10**9+7

    N=I()

    P=LI()

    Q=LI()

    

    

    import itertools

    

    cnt=0

    p=0

    q=0

    for ite in itertools.permutations(list(range(1,N+1))):

        if P==list(ite):

            p=cnt

        if Q==list(ite):

            q=cnt    

        cnt+=1

        

    print((abs(p-q)))

        

        

main()

import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
index = 0
for i in itertools.permutations(list(range(1, N + 1)), N):
    if P == list(i):
        a = index
    if Q == list(i):
        b = index
    index += 1
print((abs(b - a)))

import sys

input = sys.stdin.readline


def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def main():
    mod = 10**9 + 7
    N = I()
    P = LI()
    Q = LI()
    import itertools

    cnt = 0
    p = 0
    q = 0
    for ite in itertools.permutations(list(range(1, N + 1))):
        if P == list(ite):
            p = cnt
        if Q == list(ite):
            q = cnt
        cnt += 1
    print((abs(p - q)))


main()

-import itertools +import sys -N = int(eval(input())) -P = list(map(int, input().split())) -Q = list(map(int, input().split())) -a = 0 -b = 0 -index = 0 -for i in itertools.permutations(list(range(1, N + 1)), N): -    if P == list(i): -        a = index -    if Q == list(i): -        b = index -    index += 1 -print((abs(b - a))) +input = sys.stdin.readline + + +def I(): +    return int(eval(input())) + + +def MI(): +    return list(map(int, input().split())) + + +def LI(): +    return list(map(int, input().split())) + + +def main(): +    mod = 10**9 + 7 +    N = I() +    P = LI() +    Q = LI() +    import itertools + +    cnt = 0 +    p = 0 +    q = 0 +    for ite in itertools.permutations(list(range(1, N + 1))): +        if P == list(ite): +            p = cnt +        if Q == list(ite): +            q = cnt +        cnt += 1 +    print((abs(p - q))) + + +main()