fun main(args: Array<String>) {
  val N = readInt()
  val P = readInts()
  val Q = readInts()

  val origin = (1..N).toMutableList()
  var pIndex = 0
  var qIndex = 0
  for (i in 0 until Int.MAX_VALUE) {
    if (origin == P) pIndex = i
    if (origin == Q) qIndex = i
    if (!nextPermutation(origin)) break
  }
  println(Math.abs(pIndex - qIndex))
}

fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readInts() = readLine()!!.split(" ").map { it.toInt() }

/** 順列の列挙 c++ のやつと一緒 */
fun nextPermutation(list: MutableList<Int>): Boolean {
  for (i in list.size - 2 downTo 0) {
    if (list[i] < list[i + 1]) {
      for (j in list.size - 1 downTo 0) {
        if (list[i] < list[j]) {
          val tmp = list[i]
          list[i] = list[j]
          list[j] = tmp
          list.subList(i + 1, list.size).reverse()
          return true
        }
      }
    }
  }
  list.reverse()
  return false
}