import itertools

N=int(eval(input()))

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

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

K=list(itertools.permutations(list(range(1,N+1))))

print((abs(K.index(a)-K.index(b))))
import itertools as it

N=int(eval(input()))

W=list(range(1,N+1))

c1=-1

c2=-1

c=0

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

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

if L==M:

  print((0))

  exit()

for i in it.permutations(W,N):

  c+=1

  if i==L:

    c1=c

  elif i==M:

    c2=c

print((abs(c1-c2)))
import itertools

N = int(eval(input()))
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
K = list(itertools.permutations(list(range(1, N + 1))))
print((abs(K.index(a) - K.index(b))))

import itertools as it

N = int(eval(input()))
W = list(range(1, N + 1))
c1 = -1
c2 = -1
c = 0
L = tuple(map(int, input().split()))
M = tuple(map(int, input().split()))
if L == M:
    print((0))
    exit()
for i in it.permutations(W, N):
    c += 1
    if i == L:
        c1 = c
    elif i == M:
        c2 = c
print((abs(c1 - c2)))

-import itertools +import itertools as it -a = tuple(map(int, input().split())) -b = tuple(map(int, input().split())) -K = list(itertools.permutations(list(range(1, N + 1)))) -print((abs(K.index(a) - K.index(b)))) +W = list(range(1, N + 1)) +c1 = -1 +c2 = -1 +c = 0 +L = tuple(map(int, input().split())) +M = tuple(map(int, input().split())) +if L == M: +    print((0)) +    exit() +for i in it.permutations(W, N): +    c += 1 +    if i == L: +        c1 = c +    elif i == M: +        c2 = c +print((abs(c1 - c2)))