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

a = b = nil
[*1..N].permutation(N).each_with_index do |n,i|
  a = i + 1 if P == n
  b = i + 1 if Q == n
  break if a && b
end
puts (a-b).abs
