local n = io.read("*n")
local fact = {1}
for i = 2, n do
  fact[i] = i * fact[i - 1]
end
local function getrank(a)
  local box = {}
  for i = 1, n do box[i] = false end
  local r = 0
  for i = 1, n do
    for j = 1, a[i] - 1 do
      if not box[j] then
        r = r + fact[n - i]
      end
    end
    box[a[i]] = true
  end
  return r
end
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local ar = getrank(t)
for i = 1, n do
  t[i] = io.read("*n")
end
local br = getrank(t)
print(math.abs(ar - br))
