from itertools import permutations

n = int(eval(input()))

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

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



dict = {}

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

i = 1

for x in permutations(d,n):

    dict[x] = i

    i += 1



print((abs(dict[P]-dict[Q])))
from itertools import permutations

n = int(eval(input()))

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

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



a,b = -1,-1

for i,x in enumerate(permutations([i+1 for i in range(n)])):

    c,d = True,True

    for j in range(n):

        if x[j] != p[j]:

            c = False

        if x[j] != q[j]:

            d = False

    if c:a = i+1

    if d:b = i+1

print((abs(a-b)))
from itertools import permutations

n = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
dict = {}
d = [i for i in range(1, n + 1)]
i = 1
for x in permutations(d, n):
    dict[x] = i
    i += 1
print((abs(dict[P] - dict[Q])))

from itertools import permutations

n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a, b = -1, -1
for i, x in enumerate(permutations([i + 1 for i in range(n)])):
    c, d = True, True
    for j in range(n):
        if x[j] != p[j]:
            c = False
        if x[j] != q[j]:
            d = False
    if c:
        a = i + 1
    if d:
        b = i + 1
print((abs(a - b)))

-P = tuple(map(int, input().split())) -Q = tuple(map(int, input().split())) -dict = {} -d = [i for i in range(1, n + 1)] -i = 1 -for x in permutations(d, n): -    dict[x] = i -    i += 1 -print((abs(dict[P] - dict[Q]))) +p = list(map(int, input().split())) +q = list(map(int, input().split())) +a, b = -1, -1 +for i, x in enumerate(permutations([i + 1 for i in range(n)])): +    c, d = True, True +    for j in range(n): +        if x[j] != p[j]: +            c = False +        if x[j] != q[j]: +            d = False +    if c: +        a = i + 1 +    if d: +        b = i + 1 +print((abs(a - b)))