n = gets.chomp.to_i
p = gets.chomp.split
q = gets.chomp.split

list = '12345678'[0,n]
p_count = 1
(n-1).times do |i|
  num = list.index(p[i])
  p_count += num * (1..(n-i-1)).reduce(:*)
  list.delete!(p[i])
end

list = '12345678'[0,n]
q_count = 1
(n-1).times do |i|
  num = list.index(q[i])
  q_count += num * (1..(n-i-1)).reduce(:*)
  list.delete!(q[i])
end

puts((p_count - q_count).abs)