import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val n = sc.nextInt
  val p = Array.fill(n)(sc.nextInt).mkString
  val q = Array.fill(n)(sc.nextInt).mkString
  val listAndIndex = (1 to n).permutations.map(_.mkString).toList.sorted.zipWithIndex

  val a = listAndIndex.filter(l => l._1 == p).map(_._2).head
  val b = listAndIndex.filter(l => l._1 == q).map(_._2).head
  println(Math.abs(a - b))
}
