import itertools





def checkArrays(arr1, arr2):

    if len(arr1) != len(arr2):

        return False



    for i in range(len(arr1)):

        if arr1[i] != arr2[i]:

            return False



    return True





n = int(eval(input()))



arrays = list(itertools.permutations([i for i in range(1, n + 1)]))



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

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



a = b = 1

for i in range(len(arrays)):

    ar = list(arrays[i])

    if checkArrays(ar, p):

        a = i + 1

    if checkArrays(ar, q):

        b = i + 1



print((abs(a - b)))



import itertools



n = int(eval(input()))



array = [i for i in range(1, n + 1)]

gen_array = itertools.permutations(array, n)



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

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



idx = 1

for lis in gen_array:

    if list(lis) == a:

        num_a = idx

    if list(lis) == b:

        num_b = idx

    idx += 1



print((abs(num_a - num_b)))

import itertools


def checkArrays(arr1, arr2):
    if len(arr1) != len(arr2):
        return False
    for i in range(len(arr1)):
        if arr1[i] != arr2[i]:
            return False
    return True


n = int(eval(input()))
arrays = list(itertools.permutations([i for i in range(1, n + 1)]))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a = b = 1
for i in range(len(arrays)):
    ar = list(arrays[i])
    if checkArrays(ar, p):
        a = i + 1
    if checkArrays(ar, q):
        b = i + 1
print((abs(a - b)))

import itertools

n = int(eval(input()))
array = [i for i in range(1, n + 1)]
gen_array = itertools.permutations(array, n)
a = list(map(int, input().split()))
b = list(map(int, input().split()))
idx = 1
for lis in gen_array:
    if list(lis) == a:
        num_a = idx
    if list(lis) == b:
        num_b = idx
    idx += 1
print((abs(num_a - num_b)))

- -def checkArrays(arr1, arr2): -    if len(arr1) != len(arr2): -        return False -    for i in range(len(arr1)): -        if arr1[i] != arr2[i]: -            return False -    return True - - -arrays = list(itertools.permutations([i for i in range(1, n + 1)])) -p = list(map(int, input().split())) -q = list(map(int, input().split())) -a = b = 1 -for i in range(len(arrays)): -    ar = list(arrays[i]) -    if checkArrays(ar, p): -        a = i + 1 -    if checkArrays(ar, q): -        b = i + 1 -print((abs(a - b))) +array = [i for i in range(1, n + 1)] +gen_array = itertools.permutations(array, n) +a = list(map(int, input().split())) +b = list(map(int, input().split())) +idx = 1 +for lis in gen_array: +    if list(lis) == a: +        num_a = idx +    if list(lis) == b: +        num_b = idx +    idx += 1 +print((abs(num_a - num_b)))