import itertools,math

n = int(eval(input()))

a = sorted(list(itertools.permutations([ i for i in range(1,n+1)])))

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

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

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

n = int(eval(input()))

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

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

L = list(itertools.permutations([i for i in range(1,n+1)]))

print((

    abs((L.index(P)+1)-((L.index(Q)+1)))

    ))

import itertools, math

n = int(eval(input()))
a = sorted(list(itertools.permutations([i for i in range(1, n + 1)])))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
print((abs(a.index(p) - a.index(q))))

import itertools

n = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
L = list(itertools.permutations([i for i in range(1, n + 1)]))
print((abs((L.index(P) + 1) - ((L.index(Q) + 1)))))

-import itertools, math +import itertools -a = sorted(list(itertools.permutations([i for i in range(1, n + 1)]))) -p = tuple(map(int, input().split())) -q = tuple(map(int, input().split())) -print((abs(a.index(p) - a.index(q)))) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +L = list(itertools.permutations([i for i in range(1, n + 1)])) +print((abs((L.index(P) + 1) - ((L.index(Q) + 1)))))