object Main extends App {
  @scala.annotation.tailrec
  def loop(ints: Seq[Int], acc: Seq[Int], indecies: Seq[Int]): Seq[Int] =
    ints match {
      case Seq() => indecies
      case x :: xs => loop(xs, acc :+ x, indecies :+ (x - acc.count(_ < x) - 1))
    }
  def countOrder(ints: Seq[Int], factorials: Seq[Int]): Int =
    loop(ints, Vector.empty, Vector.empty)
      .zip(factorials)
      .map {
        case (index, factorial) => index * factorial
      }
      .sum
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val P, Q = List.fill(n)(sc.nextInt())
  val factorials: Seq[Int] = (2 until n).scanLeft(1)(_ * _).reverse
  println(math.abs(countOrder(P, factorials) - countOrder(Q, factorials)))
}
