from itertools import *

n = int(eval(input()))

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

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



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

for i,per in enumerate(list(permutations(a))):

    if per == p:

        x = i

    if per == q:

        y = i

print((abs(x-y)))

N = int(eval(input()))

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

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

L = [i+1 for i in range(N)]

R = [i+1 for i in range(N)]

A,B = 0,0

for i in range(N-1):

    A *= N-i

    B *= N-i

    A += L.index(P[i])

    B += R.index(Q[i])

    L[L.index(P[i])] = 100

    R[R.index(Q[i])] = 100

    L.sort()

    R.sort()

print((abs(A-B)))

from itertools import *

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
a = [i + 1 for i in range(n)]
for i, per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print((abs(x - y)))

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = [i + 1 for i in range(N)]
R = [i + 1 for i in range(N)]
A, B = 0, 0
for i in range(N - 1):
    A *= N - i
    B *= N - i
    A += L.index(P[i])
    B += R.index(Q[i])
    L[L.index(P[i])] = 100
    R[R.index(Q[i])] = 100
    L.sort()
    R.sort()
print((abs(A - B)))

-from itertools import * - -n = int(eval(input())) -p = tuple(map(int, input().split())) -q = tuple(map(int, input().split())) -a = [i + 1 for i in range(n)] -for i, per in enumerate(list(permutations(a))): -    if per == p: -        x = i -    if per == q: -        y = i -print((abs(x - y))) +N = int(eval(input())) +P = list(map(int, input().split())) +Q = list(map(int, input().split())) +L = [i + 1 for i in range(N)] +R = [i + 1 for i in range(N)] +A, B = 0, 0 +for i in range(N - 1): +    A *= N - i +    B *= N - i +    A += L.index(P[i]) +    B += R.index(Q[i]) +    L[L.index(P[i])] = 100 +    R[R.index(Q[i])] = 100 +    L.sort() +    R.sort() +print((abs(A - B)))