import sys

from itertools import permutations



n = int(sys.stdin.readline().rstrip())

p, q = [tuple(map(int, sys.stdin.readline().split())) for _ in range(2)]



def main():

    perms = list(permutations(list(range(1, n+1))))

    return abs(perms.index(p) - perms.index(q))



if __name__ == '__main__':

    ans = main()

    print(ans)
import sys



n = int(sys.stdin.readline().rstrip())

*p, = list(map(int, sys.stdin.readline().split()))

*q, = list(map(int, sys.stdin.readline().split()))



factorial = [1] * (n + 1)

for i in range(n): factorial[i+1] = factorial[i] * (i + 1)



def main():

    d = 0

    a = [0] * (n + 1); b = [0] * (n + 1)

    for i in range(n - 1):

        a[p[i]] = b[q[i]] = 1

        c1 = c2 = 0

        for j in range(1, p[i]): c1 += a[j]

        for j in range(1, q[i]): c2 += b[j]

        d += ((q[i] - c2) - (p[i] - c1)) * factorial[n-(i+1)]

    print((abs(d)))



if __name__ ==  '__main__':

    main()
import sys
from itertools import permutations

n = int(sys.stdin.readline().rstrip())
p, q = [tuple(map(int, sys.stdin.readline().split())) for _ in range(2)]


def main():
    perms = list(permutations(list(range(1, n + 1))))
    return abs(perms.index(p) - perms.index(q))


if __name__ == "__main__":
    ans = main()
    print(ans)

import sys

n = int(sys.stdin.readline().rstrip())
(*p,) = list(map(int, sys.stdin.readline().split()))
(*q,) = list(map(int, sys.stdin.readline().split()))
factorial = [1] * (n + 1)
for i in range(n):
    factorial[i + 1] = factorial[i] * (i + 1)


def main():
    d = 0
    a = [0] * (n + 1)
    b = [0] * (n + 1)
    for i in range(n - 1):
        a[p[i]] = b[q[i]] = 1
        c1 = c2 = 0
        for j in range(1, p[i]):
            c1 += a[j]
        for j in range(1, q[i]):
            c2 += b[j]
        d += ((q[i] - c2) - (p[i] - c1)) * factorial[n - (i + 1)]
    print((abs(d)))


if __name__ == "__main__":
    main()

-from itertools import permutations -p, q = [tuple(map(int, sys.stdin.readline().split())) for _ in range(2)] +(*p,) = list(map(int, sys.stdin.readline().split())) +(*q,) = list(map(int, sys.stdin.readline().split())) +factorial = [1] * (n + 1) +for i in range(n): +    factorial[i + 1] = factorial[i] * (i + 1) -    perms = list(permutations(list(range(1, n + 1)))) -    return abs(perms.index(p) - perms.index(q)) +    d = 0 +    a = [0] * (n + 1) +    b = [0] * (n + 1) +    for i in range(n - 1): +        a[p[i]] = b[q[i]] = 1 +        c1 = c2 = 0 +        for j in range(1, p[i]): +            c1 += a[j] +        for j in range(1, q[i]): +            c2 += b[j] +        d += ((q[i] - c2) - (p[i] - c1)) * factorial[n - (i + 1)] +    print((abs(d))) -    ans = main() -    print(ans) +    main()