// abc092-b.scala
object Main extends App {
  val n = io.StdIn.readInt
  val ps = io.StdIn.readLine.split(" ").map(_.toInt)
  val qs = io.StdIn.readLine.split(" ").map(_.toInt)

  val permutations = (1 to n).permutations.toArray
  val result = (permutations.indexOf(ps) - permutations.indexOf(qs)).abs
  println(result)
}