# https://atcoder.jp/contests/abc150/tasks/abc150_c

def init_fact(n)
  @fact = Array.new(n + 1)
  @fact[0] = 1
  n.times { |i| @fact[i + 1] = @fact[i] * (i + 1) }
end

def order(ps)
  order2((1..ps.size).to_a, ps)
end

def order2(as, ps)
  return 0 if as.size == 0
  i = ps[0]
  ret = as.index(i) * @fact[as.size - 1]
  as.delete(i)
  ps.delete(i)
  ret + order2(as, ps)
end

N = gets.to_i
Ps = gets.split.map(&:to_i)
Qs = gets.split.map(&:to_i)

init_fact(N)
a = order(Ps)
b = order(Qs)
puts (a - b).abs
