N = int(eval(input()))

P = [int(i) for i in input().split(' ')]

Q = [int(i) for i in input().split(' ')]



fact = [1] * 8

for i in range(1,8):

    fact[i] = fact[i-1] * i



a = 0

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



for i , v  in enumerate(P):

    num = remain.index(v)

    remain.remove(v)

    a += num * fact[(N - i - 1)]



b = 0

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



for i , v  in enumerate(Q):

    num = remain.index(v)

    remain.remove(v)

    b += num * fact[(N - i - 1)]



print((abs(b-a)))

N = int(eval(input()))

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

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



fact = [1] *(N+1)

for i in range(1,N+1):

    fact[i] = fact[i-1] *i

a = 1

b = 1

P2 = sorted(P)

Q2 = sorted(Q)

for i,(p,q) in enumerate(zip(P,Q)):

    a += P2.index(p)*fact[N-i-1]

    b += Q2.index(q)*fact[N-i-1]

    P2.remove(p)

    Q2.remove(q)

print((abs(b-a)))



N = int(eval(input()))
P = [int(i) for i in input().split(" ")]
Q = [int(i) for i in input().split(" ")]
fact = [1] * 8
for i in range(1, 8):
    fact[i] = fact[i - 1] * i
a = 0
remain = list(range(1, N + 1))
for i, v in enumerate(P):
    num = remain.index(v)
    remain.remove(v)
    a += num * fact[(N - i - 1)]
b = 0
remain = list(range(1, N + 1))
for i, v in enumerate(Q):
    num = remain.index(v)
    remain.remove(v)
    b += num * fact[(N - i - 1)]
print((abs(b - a)))

N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
fact = [1] * (N + 1)
for i in range(1, N + 1):
    fact[i] = fact[i - 1] * i
a = 1
b = 1
P2 = sorted(P)
Q2 = sorted(Q)
for i, (p, q) in enumerate(zip(P, Q)):
    a += P2.index(p) * fact[N - i - 1]
    b += Q2.index(q) * fact[N - i - 1]
    P2.remove(p)
    Q2.remove(q)
print((abs(b - a)))

-P = [int(i) for i in input().split(" ")] -Q = [int(i) for i in input().split(" ")] -fact = [1] * 8 -for i in range(1, 8): +P = list(map(int, input().split())) +Q = list(map(int, input().split())) +fact = [1] * (N + 1) +for i in range(1, N + 1): -a = 0 -remain = list(range(1, N + 1)) -for i, v in enumerate(P): -    num = remain.index(v) -    remain.remove(v) -    a += num * fact[(N - i - 1)] -b = 0 -remain = list(range(1, N + 1)) -for i, v in enumerate(Q): -    num = remain.index(v) -    remain.remove(v) -    b += num * fact[(N - i - 1)] +a = 1 +b = 1 +P2 = sorted(P) +Q2 = sorted(Q) +for i, (p, q) in enumerate(zip(P, Q)): +    a += P2.index(p) * fact[N - i - 1] +    b += Q2.index(q) * fact[N - i - 1] +    P2.remove(p) +    Q2.remove(q)