N = gets.to_i
P = gets.split.map!(&:to_i)
Q = gets.split.map!(&:to_i)


a = 1
b = 1
(1..N).to_a.permutation(N).with_index(1) do |d, i|
  if P == d
    a = i
  elsif Q == d
    b = i
  end
end

puts (a-b).abs
