import itertools



N = int(eval(input()))

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

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



count = 1

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



    if p == P:

        count_P = count

    if p == Q:

        count_Q = count



    count += 1



ans = abs(count_P - count_Q)

print(ans)
import itertools



N = int(eval(input()))

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

tuple_P = tuple(P)

sorted_P = sorted(P)

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

tuple_Q = tuple(Q)

sorted_Q = sorted(Q)



count_P = 1

for perm_P in itertools.permutations(sorted_P):

    if tuple_P == perm_P:

        break

    count_P += 1



count_Q = 1

for perm_Q in itertools.permutations(sorted_Q):

    if tuple_Q == perm_Q:

        break

    count_Q += 1



answer = abs(count_P - count_Q)

print(answer)
import itertools

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
count = 1
for p in itertools.permutations(list(range(1, N + 1))):
    if p == P:
        count_P = count
    if p == Q:
        count_Q = count
    count += 1
ans = abs(count_P - count_Q)
print(ans)

import itertools

N = int(eval(input()))
P = list(map(int, input().split()))
tuple_P = tuple(P)
sorted_P = sorted(P)
Q = list(map(int, input().split()))
tuple_Q = tuple(Q)
sorted_Q = sorted(Q)
count_P = 1
for perm_P in itertools.permutations(sorted_P):
    if tuple_P == perm_P:
        break
    count_P += 1
count_Q = 1
for perm_Q in itertools.permutations(sorted_Q):
    if tuple_Q == perm_Q:
        break
    count_Q += 1
answer = abs(count_P - count_Q)
print(answer)

-P = tuple(map(int, input().split())) -Q = tuple(map(int, input().split())) -count = 1 -for p in itertools.permutations(list(range(1, N + 1))): -    if p == P: -        count_P = count -    if p == Q: -        count_Q = count -    count += 1 -ans = abs(count_P - count_Q) -print(ans) +P = list(map(int, input().split())) +tuple_P = tuple(P) +sorted_P = sorted(P) +Q = list(map(int, input().split())) +tuple_Q = tuple(Q) +sorted_Q = sorted(Q) +count_P = 1 +for perm_P in itertools.permutations(sorted_P): +    if tuple_P == perm_P: +        break +    count_P += 1 +count_Q = 1 +for perm_Q in itertools.permutations(sorted_Q): +    if tuple_Q == perm_Q: +        break +    count_Q += 1 +answer = abs(count_P - count_Q) +print(answer)