import itertools



N = int(eval(input()))

P = tuple([int(p) for p in input().split()])

Q = tuple([int(q) for q in input().split()])

a, b = -1, -1

count = 1

for seq in itertools.permutations(tuple(range(1, N+1))):

    if seq == P:

        a = count

    if seq == Q:

        b = count

    if a != -1 and b != -1:

        break

    count += 1

print((abs(a-b)))
import sys

import itertools





input = sys.stdin.readline





def main():

    a, b = -1, -1

    count = 0

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

        if p == P:

            a = count

        if p == Q:

            b = count

        if a != -1 and b != -1:

            break

        count += 1

    print((abs(a-b)))





if __name__ == "__main__":

    N = int(eval(input()))

    P = tuple([int(p) for p in input().split()])

    Q = tuple([int(q) for q in input().split()])

    main()

import itertools

N = int(eval(input()))
P = tuple([int(p) for p in input().split()])
Q = tuple([int(q) for q in input().split()])
a, b = -1, -1
count = 1
for seq in itertools.permutations(tuple(range(1, N + 1))):
    if seq == P:
        a = count
    if seq == Q:
        b = count
    if a != -1 and b != -1:
        break
    count += 1
print((abs(a - b)))

import sys
import itertools

input = sys.stdin.readline


def main():
    a, b = -1, -1
    count = 0
    for p in itertools.permutations(list(range(1, N + 1))):
        if p == P:
            a = count
        if p == Q:
            b = count
        if a != -1 and b != -1:
            break
        count += 1
    print((abs(a - b)))


if __name__ == "__main__":
    N = int(eval(input()))
    P = tuple([int(p) for p in input().split()])
    Q = tuple([int(q) for q in input().split()])
    main()

+import sys -N = int(eval(input())) -P = tuple([int(p) for p in input().split()]) -Q = tuple([int(q) for q in input().split()]) -a, b = -1, -1 -count = 1 -for seq in itertools.permutations(tuple(range(1, N + 1))): -    if seq == P: -        a = count -    if seq == Q: -        b = count -    if a != -1 and b != -1: -        break -    count += 1 -print((abs(a - b))) +input = sys.stdin.readline + + +def main(): +    a, b = -1, -1 +    count = 0 +    for p in itertools.permutations(list(range(1, N + 1))): +        if p == P: +            a = count +        if p == Q: +            b = count +        if a != -1 and b != -1: +            break +        count += 1 +    print((abs(a - b))) + + +if __name__ == "__main__": +    N = int(eval(input())) +    P = tuple([int(p) for p in input().split()]) +    Q = tuple([int(q) for q in input().split()]) +    main()