from itertools import permutations

n=int(eval(input()))

p=tuple(input().split())

q=tuple(input().split())

a=sorted(list(permutations(p)))

print((abs(a.index(p)-a.index(q))))
from itertools import permutations

n=int(eval(input()))

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

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

a=list(permutations(list(range(1,n+1))))

print((abs(a.index(p)-a.index(q))))
from itertools import permutations

n = int(eval(input()))
p = tuple(input().split())
q = tuple(input().split())
a = sorted(list(permutations(p)))
print((abs(a.index(p) - a.index(q))))

from itertools import permutations

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
a = list(permutations(list(range(1, n + 1))))
print((abs(a.index(p) - a.index(q))))

-p = tuple(input().split()) -q = tuple(input().split()) -a = sorted(list(permutations(p))) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +a = list(permutations(list(range(1, n + 1))))