n = int(eval(input()))

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

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

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

a = 0

b = 0

from itertools import permutations

per = list(permutations(seq,n))

for i in range(len(per)):

    if per[i] == p:

        a = i

    if per[i] == q:

        b = i

print((abs(a-b)))
n = int(eval(input()))

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

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

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

# a = 0

# b = 0

from itertools import permutations

per = list(permutations(seq,n))

# for i in range(len(per)):

#     if per[i] == p:

#         a = i

#     if per[i] == q:

#         b = i

print((abs(per.index(p)-per.index(q))))
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
seq = [i + 1 for i in range(n)]
a = 0
b = 0
from itertools import permutations

per = list(permutations(seq, n))
for i in range(len(per)):
    if per[i] == p:
        a = i
    if per[i] == q:
        b = i
print((abs(a - b)))

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
seq = [i + 1 for i in range(n)]
# a = 0
# b = 0
from itertools import permutations

per = list(permutations(seq, n))
# for i in range(len(per)):
#     if per[i] == p:
#         a = i
#     if per[i] == q:
#         b = i
print((abs(per.index(p) - per.index(q))))

-a = 0 -b = 0 +# a = 0 +# b = 0 -for i in range(len(per)): -    if per[i] == p: -        a = i -    if per[i] == q: -        b = i -print((abs(a - b))) +# for i in range(len(per)): +#     if per[i] == p: +#         a = i +#     if per[i] == q: +#         b = i +print((abs(per.index(p) - per.index(q))))