N = int(eval(input()))



def factorial(num):

    if num == 1:

        return 1

    return num * factorial(num - 1)



def count_order(group):

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

    res = 1

    for i in range(len(group) - 1):

        num = group[i]

        res += numbers.index(num) * pattern[-(i + 1)]

        numbers.pop(numbers.index(num))

    return res





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

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



pattern = [factorial(i) for i in range(1, N)]

res_p = count_order(P)

res_q = count_order(Q)



print((abs(res_p - res_q)))

from itertools import permutations

n  = int(eval(input()))

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

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

comb = list(permutations([i for i in range(1, n + 1)]))

comb.sort()

a = 0

b = 0



for i in range(len(comb)):

    if list(comb[i]) == P:

        a = i

    if list(comb[i]) == Q:

        b = i

ans = abs(a - b)

print(ans)
N = int(eval(input()))


def factorial(num):
    if num == 1:
        return 1
    return num * factorial(num - 1)


def count_order(group):
    numbers = [i for i in range(1, N + 1)]
    res = 1
    for i in range(len(group) - 1):
        num = group[i]
        res += numbers.index(num) * pattern[-(i + 1)]
        numbers.pop(numbers.index(num))
    return res


P = list(map(int, input().split()))
Q = list(map(int, input().split()))
pattern = [factorial(i) for i in range(1, N)]
res_p = count_order(P)
res_q = count_order(Q)
print((abs(res_p - res_q)))

from itertools import permutations

n = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
comb = list(permutations([i for i in range(1, n + 1)]))
comb.sort()
a = 0
b = 0
for i in range(len(comb)):
    if list(comb[i]) == P:
        a = i
    if list(comb[i]) == Q:
        b = i
ans = abs(a - b)
print(ans)

-N = int(eval(input())) +from itertools import permutations - -def factorial(num): -    if num == 1: -        return 1 -    return num * factorial(num - 1) - - -def count_order(group): -    numbers = [i for i in range(1, N + 1)] -    res = 1 -    for i in range(len(group) - 1): -        num = group[i] -        res += numbers.index(num) * pattern[-(i + 1)] -        numbers.pop(numbers.index(num)) -    return res - - +n = int(eval(input())) -pattern = [factorial(i) for i in range(1, N)] -res_p = count_order(P) -res_q = count_order(Q) -print((abs(res_p - res_q))) +comb = list(permutations([i for i in range(1, n + 1)])) +comb.sort() +a = 0 +b = 0 +for i in range(len(comb)): +    if list(comb[i]) == P: +        a = i +    if list(comb[i]) == Q: +        b = i +ans = abs(a - b) +print(ans)