import itertools

(n,),p,q=[tuple(map(int, i.split())) for i in open(0)]

j=list(itertools.permutations(list(range(1, -~n))))

print((abs(j.index(p) - j.index(q))))
import math

(n,),p,q=[list(map(int, i.split())) for i in open(0)]

def c(l):

    s=0

    t=list(range(1,n+1))

    for i in range(n):

        s+=t.index(l[i])*math.factorial(n-i-1)

        t.remove(l[i])

    return s

print((abs(c(p)-c(q))))
import itertools

(n,), p, q = [tuple(map(int, i.split())) for i in open(0)]
j = list(itertools.permutations(list(range(1, -~n))))
print((abs(j.index(p) - j.index(q))))

import math

(n,), p, q = [list(map(int, i.split())) for i in open(0)]


def c(l):
    s = 0
    t = list(range(1, n + 1))
    for i in range(n):
        s += t.index(l[i]) * math.factorial(n - i - 1)
        t.remove(l[i])
    return s


print((abs(c(p) - c(q))))

-import itertools +import math -(n,), p, q = [tuple(map(int, i.split())) for i in open(0)] -j = list(itertools.permutations(list(range(1, -~n)))) -print((abs(j.index(p) - j.index(q)))) +(n,), p, q = [list(map(int, i.split())) for i in open(0)] + + +def c(l): +    s = 0 +    t = list(range(1, n + 1)) +    for i in range(n): +        s += t.index(l[i]) * math.factorial(n - i - 1) +        t.remove(l[i]) +    return s + + +print((abs(c(p) - c(q))))