import itertools



n = int(eval(input()))



order = list(itertools.permutations([int(x)for x in range(1,n+1)]))

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

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



for i in range(len(order)):

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

        P_num = i

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

        Q_num = i

        

print((abs(P_num-Q_num)))
import sys

import itertools



input = sys.stdin.readline



def slove():

    n = int(eval(input()))

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

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

    """

    print(P)

    print(Q)

    """

    count = 0

    for item in itertools.permutations(list(range(1,1+n))):

        if P == item:

            a = count

        if Q == item:

            b = count

        count += 1

    print((abs(a-b)))





if __name__ == "__main__":

    slove()

import itertools

n = int(eval(input()))
order = list(itertools.permutations([int(x) for x in range(1, n + 1)]))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
for i in range(len(order)):
    if P == list(order[i]):
        P_num = i
    if Q == list(order[i]):
        Q_num = i
print((abs(P_num - Q_num)))

import sys
import itertools

input = sys.stdin.readline


def slove():
    n = int(eval(input()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    """
    print(P)
    print(Q)
    """
    count = 0
    for item in itertools.permutations(list(range(1, 1 + n))):
        if P == item:
            a = count
        if Q == item:
            b = count
        count += 1
    print((abs(a - b)))


if __name__ == "__main__":
    slove()

+import sys -n = int(eval(input())) -order = list(itertools.permutations([int(x) for x in range(1, n + 1)])) -P = [int(x) for x in input().split()] -Q = [int(x) for x in input().split()] -for i in range(len(order)): -    if P == list(order[i]): -        P_num = i -    if Q == list(order[i]): -        Q_num = i -print((abs(P_num - Q_num))) +input = sys.stdin.readline + + +def slove(): +    n = int(eval(input())) +    P = tuple(map(int, input().split())) +    Q = tuple(map(int, input().split())) +    """ +    print(P) +    print(Q) +    """ +    count = 0 +    for item in itertools.permutations(list(range(1, 1 + n))): +        if P == item: +            a = count +        if Q == item: +            b = count +        count += 1 +    print((abs(a - b))) + + +if __name__ == "__main__": +    slove()