def factorial(x)
  product = 1
  for i in 1..x do
    product *= i
  end
  return product
end

n = gets.to_i
p = gets.split.map(&:to_i)
q = gets.split.map(&:to_i)
pp = (1..n).to_a
qq = (1..n).to_a
a = 0
b = 0
for i in 0...n do
  a += pp.index(p[i]) * factorial(n-i-1)
  b += qq.index(q[i]) * factorial(n-i-1)
  pp.delete(p[i])
  qq.delete(q[i])
end
puts (a-b).abs
