from collections import deque



def solve(N, P, Q):

    stack = deque()

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

    for idx, next_val in enumerate(vals):

        stack.append([[next_val], vals[: idx] + vals[idx + 1:]])

    a, b = -1, -1

    counter = 0

    while stack:

        cur_list, rest = stack.popleft()

        if not rest:

            counter += 1

            a = counter if cur_list == P else a

            b = counter if cur_list == Q else b

            if 0 < a and 0 < b:

                print((abs(a - b)))

                return



        for idx, next_val in enumerate(rest):

            stack.append([cur_list + [next_val], rest[: idx] + rest[idx + 1:]])



if __name__ == "__main__":

    N = int(eval(input()))

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

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

    solve(N, P, Q)

from itertools import permutations



def solve(N, P, Q):

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

    perms = list(permutations(vals))

    print((abs(perms.index(P) - perms.index(Q))))



if __name__ == "__main__":

    N = int(eval(input()))

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

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

    solve(N, P, Q)

from collections import deque


def solve(N, P, Q):
    stack = deque()
    vals = [i for i in range(1, N + 1)]
    for idx, next_val in enumerate(vals):
        stack.append([[next_val], vals[:idx] + vals[idx + 1 :]])
    a, b = -1, -1
    counter = 0
    while stack:
        cur_list, rest = stack.popleft()
        if not rest:
            counter += 1
            a = counter if cur_list == P else a
            b = counter if cur_list == Q else b
            if 0 < a and 0 < b:
                print((abs(a - b)))
                return
        for idx, next_val in enumerate(rest):
            stack.append([cur_list + [next_val], rest[:idx] + rest[idx + 1 :]])


if __name__ == "__main__":
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    solve(N, P, Q)

from itertools import permutations


def solve(N, P, Q):
    vals = [i for i in range(1, N + 1)]
    perms = list(permutations(vals))
    print((abs(perms.index(P) - perms.index(Q))))


if __name__ == "__main__":
    N = int(eval(input()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    solve(N, P, Q)

-from collections import deque +from itertools import permutations -    stack = deque() -    for idx, next_val in enumerate(vals): -        stack.append([[next_val], vals[:idx] + vals[idx + 1 :]]) -    a, b = -1, -1 -    counter = 0 -    while stack: -        cur_list, rest = stack.popleft() -        if not rest: -            counter += 1 -            a = counter if cur_list == P else a -            b = counter if cur_list == Q else b -            if 0 < a and 0 < b: -                print((abs(a - b))) -                return -        for idx, next_val in enumerate(rest): -            stack.append([cur_list + [next_val], rest[:idx] + rest[idx + 1 :]]) +    perms = list(permutations(vals)) +    print((abs(perms.index(P) - perms.index(Q)))) -    P = list(map(int, input().split())) -    Q = list(map(int, input().split())) +    P = tuple(map(int, input().split())) +    Q = tuple(map(int, input().split()))