import itertools as i

eval(input())

f=lambda:tuple(map(int,input().split()))

P=f()

Q=f()

x=sorted(list(i.permutations(P)))

print((abs(-~x.index(P)--~x.index(Q))))
import itertools as i

f=lambda:tuple(map(int,input().split()))

x=list(i.permutations(list(range(1,int(eval(input()))+1))))

print((abs(-~x.index(f())--~x.index(f()))))
import itertools as i

eval(input())
f = lambda: tuple(map(int, input().split()))
P = f()
Q = f()
x = sorted(list(i.permutations(P)))
print((abs(-~x.index(P) - -~x.index(Q))))

import itertools as i

f = lambda: tuple(map(int, input().split()))
x = list(i.permutations(list(range(1, int(eval(input())) + 1))))
print((abs(-~x.index(f()) - -~x.index(f()))))

-eval(input()) -P = f() -Q = f() -x = sorted(list(i.permutations(P))) -print((abs(-~x.index(P) - -~x.index(Q)))) +x = list(i.permutations(list(range(1, int(eval(input())) + 1)))) +print((abs(-~x.index(f()) - -~x.index(f()))))