object Main extends App {
  import scala.math.abs
  
  val n = (1 to readInt).permutations.toList.map{_.mkString}
  val p = readLine.split(" ").mkString
  val q = readLine.split(" ").mkString
  
  println(abs(n.indexOf(p)-n.indexOf(q)))
}