import itertools

n = int(eval(input()))

a = tuple(i for i in range(1, n+1))

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

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



for i, z in enumerate(tuple(itertools.permutations(a))):

    if z == x:

        x_count = i

    if z == y:

        y_count = i

print((abs(x_count - y_count)))
from itertools import permutations

N = int(eval(input()))

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

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

comb = sorted(list(permutations([i for i in range(1, N+1)], N)))

print((abs((comb.index(P) + 1) - (comb.index(Q) + 1))))
import itertools

n = int(eval(input()))
a = tuple(i for i in range(1, n + 1))
x = tuple(map(int, input().split()))
y = tuple(map(int, input().split()))
for i, z in enumerate(tuple(itertools.permutations(a))):
    if z == x:
        x_count = i
    if z == y:
        y_count = i
print((abs(x_count - y_count)))

from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
comb = sorted(list(permutations([i for i in range(1, N + 1)], N)))
print((abs((comb.index(P) + 1) - (comb.index(Q) + 1))))

-import itertools +from itertools import permutations -n = int(eval(input())) -a = tuple(i for i in range(1, n + 1)) -x = tuple(map(int, input().split())) -y = tuple(map(int, input().split())) -for i, z in enumerate(tuple(itertools.permutations(a))): -    if z == x: -        x_count = i -    if z == y: -        y_count = i -print((abs(x_count - y_count))) +N = int(eval(input())) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +comb = sorted(list(permutations([i for i in range(1, N + 1)], N))) +print((abs((comb.index(P) + 1) - (comb.index(Q) + 1))))