n = int(eval(input()))

#a,b = map(int,input().split())

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

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

L = [x+1 for x in range(n)]



li = []

import itertools

for x in itertools.permutations(L,n):

    #print(x)

    li.append(x)

#print(li)

print((abs(li.index(L1)-li.index(L2))))
n = int(eval(input()))

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

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

l = tuple(x+1 for x in range(n))

#print(a,b,l)

c,d = 0,0

cnt = 0

from itertools import permutations

for k in permutations(l):

    #print(k)

    cnt += 1

    if a ==k:

        c = cnt

    if b ==k:

        d = cnt

print((abs(c-d)))
n = int(eval(input()))
# a,b = map(int,input().split())
L1 = tuple(map(int, input().split()))
L2 = tuple(map(int, input().split()))
L = [x + 1 for x in range(n)]
li = []
import itertools

for x in itertools.permutations(L, n):
    # print(x)
    li.append(x)
# print(li)
print((abs(li.index(L1) - li.index(L2))))

n = int(eval(input()))
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
l = tuple(x + 1 for x in range(n))
# print(a,b,l)
c, d = 0, 0
cnt = 0
from itertools import permutations

for k in permutations(l):
    # print(k)
    cnt += 1
    if a == k:
        c = cnt
    if b == k:
        d = cnt
print((abs(c - d)))

-# a,b = map(int,input().split()) -L1 = tuple(map(int, input().split())) -L2 = tuple(map(int, input().split())) -L = [x + 1 for x in range(n)] -li = [] -import itertools +a = tuple(map(int, input().split())) +b = tuple(map(int, input().split())) +l = tuple(x + 1 for x in range(n)) +# print(a,b,l) +c, d = 0, 0 +cnt = 0 +from itertools import permutations -for x in itertools.permutations(L, n): -    # print(x) -    li.append(x) -# print(li) -print((abs(li.index(L1) - li.index(L2)))) +for k in permutations(l): +    # print(k) +    cnt += 1 +    if a == k: +        c = cnt +    if b == k: +        d = cnt +print((abs(c - d)))