import itertools

n = int(eval(input()))

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

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

li = [i+1 for i in range(n)]

# print(p)

# print(q)



i = 0

for elm in itertools.permutations(li):

    #print(list(elm))

    if list(elm) == p:

        mm1 = i

    if list(elm) == q:

        mm2 = i

    i+=1

print((abs(mm1-mm2)))
#n = int(input())

#n,k = map(int,input().split())

#x = list(map(int,input().split()))



import itertools



N = int(eval(input()))

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

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



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



cnt = 0

for i in itertools.permutations(li):

    if list(i) == P:

        a = cnt

    if list(i) == Q:

        b = cnt

    cnt += 1



print((abs(a-b)))













import itertools

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
li = [i + 1 for i in range(n)]
# print(p)
# print(q)
i = 0
for elm in itertools.permutations(li):
    # print(list(elm))
    if list(elm) == p:
        mm1 = i
    if list(elm) == q:
        mm2 = i
    i += 1
print((abs(mm1 - mm2)))

# n = int(input())
# n,k = map(int,input().split())
# x = list(map(int,input().split()))
import itertools

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

+# n = int(input()) +# n,k = map(int,input().split()) +# x = list(map(int,input().split())) -n = int(eval(input())) -p = list(map(int, input().split())) -q = list(map(int, input().split())) -li = [i + 1 for i in range(n)] -# print(p) -# print(q) -i = 0 -for elm in itertools.permutations(li): -    # print(list(elm)) -    if list(elm) == p: -        mm1 = i -    if list(elm) == q: -        mm2 = i -    i += 1 -print((abs(mm1 - mm2))) +N = int(eval(input())) +P = list(map(int, input().split())) +Q = list(map(int, input().split())) +li = [i + 1 for i in range(N)] +cnt = 0 +for i in itertools.permutations(li): +    if list(i) == P: +        a = cnt +    if list(i) == Q: +        b = cnt +    cnt += 1 +print((abs(a - b)))