import itertools, sys



N = int(eval(input()))

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



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

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



if A == B:

    print('0')

    sys.exit()

for v, i  in enumerate(list(itertools.permutations(N_list))):

    i = list(i)

    if A == i:

        a = v

    elif B == i:

        b = v



print((abs(a-b)))

import itertools



N = int(eval(input()))

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



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

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



N_pattern = list(itertools.permutations(N_list))



print((abs(N_pattern.index(A) - N_pattern.index(B))))

import itertools, sys

N = int(eval(input()))
N_list = [i for i in range(1, N + 1)]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if A == B:
    print("0")
    sys.exit()
for v, i in enumerate(list(itertools.permutations(N_list))):
    i = list(i)
    if A == i:
        a = v
    elif B == i:
        b = v
print((abs(a - b)))

import itertools

N = int(eval(input()))
N_list = [i for i in range(1, N + 1)]
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
N_pattern = list(itertools.permutations(N_list))
print((abs(N_pattern.index(A) - N_pattern.index(B))))

-import itertools, sys +import itertools -A = list(map(int, input().split())) -B = list(map(int, input().split())) -if A == B: -    print("0") -    sys.exit() -for v, i in enumerate(list(itertools.permutations(N_list))): -    i = list(i) -    if A == i: -        a = v -    elif B == i: -        b = v -print((abs(a - b))) +A = tuple(map(int, input().split())) +B = tuple(map(int, input().split())) +N_pattern = list(itertools.permutations(N_list)) +print((abs(N_pattern.index(A) - N_pattern.index(B))))