N = gets.to_i
P = gets.strip.gsub(' ', '')
Q = gets.strip.gsub(' ', '')

p = 0
q = 0
(1..N).to_a.permutation(N).each_with_index { |a, i|
    aa = a.join('')
    p = i if aa == P
    q = i if aa == Q
}

puts (p-q).abs
