n = int(eval(input()))

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

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

import itertools

i = 1

for ptn in itertools.permutations(list(range(1,n+1))):

    if list(ptn)==p:

      a = i

    if list(ptn)==q:

      b = i

    i += 1

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

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

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

from itertools import permutations

l = list(permutations(list(range(1,N+1))))

print((abs(l.index(P) - l.index(Q))))
n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
import itertools

i = 1
for ptn in itertools.permutations(list(range(1, n + 1))):
    if list(ptn) == p:
        a = i
    if list(ptn) == q:
        b = i
    i += 1
print((abs(a - b)))

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
from itertools import permutations

l = list(permutations(list(range(1, N + 1))))
print((abs(l.index(P) - l.index(Q))))

-n = int(eval(input())) -p = list(map(int, input().split())) -q = list(map(int, input().split())) -import itertools +N = int(eval(input())) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +from itertools import permutations -i = 1 -for ptn in itertools.permutations(list(range(1, n + 1))): -    if list(ptn) == p: -        a = i -    if list(ptn) == q: -        b = i -    i += 1 -print((abs(a - b))) +l = list(permutations(list(range(1, N + 1)))) +print((abs(l.index(P) - l.index(Q))))