def main(str)
  lines = str.split("\n").map { |x| x.split(" ").map(&:to_i) }
  n = lines[0][0]
  ps = lines[1]
  qs = lines[2]
  perm = []
  sub(perm, n, [], n.times.map { |i| i + 1 })
  ((perm.find_index { |x| x == qs } - perm.find_index { |x| x == ps })).abs
end

def sub(ret, n, cur, src)
  if cur.count == n
    ret << cur
    return
  end
  src.each do |i|
    dst = src.clone
    dst.delete(i)
    sub(ret, n, cur + [i], dst)
  end
end

unless ENV["TEST"]
  puts(main($stdin.read.chomp))
  exit(0)
end

require 'minitest/autorun'

class TestA < Minitest::Test
  def test_main
    assert_equal(3, main("3
1 3 2
3 1 2"))
    assert_equal(17517, main("8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1"))
    assert_equal(0, main("3
1 2 3
1 2 3"))
  end
end


