import java.lang.Math.abs

fun main(args: Array<String>) = p16()

fun p16() {
    val n = readLine()!!.toInt()
    val pList = readLine()!!.split(' ').map { it.toLong() }
    val qList = readLine()!!.split(' ').map { it.toLong() }

    val array = LongArray(n) { it + 1L }
    var pi = -1
    var qi = -1
    var i = 0

    do {
        val list = array.toList()
        if (list == pList) pi = i
        if (list == qList) qi = i
        i++
    } while (nextPermutation(array))

    val answer = abs(pi - qi)

    println(answer)
}

private fun nextPermutation(array: LongArray): Boolean {
    var i = array.size - 1
    while (i > 0 && array[i - 1] >= array[i]) i--
    if (i <= 0) return false
    var j = array.size - 1
    while (array[j] <= array[i - 1]) j--
    var temp = array[i - 1]
    array[i - 1] = array[j]
    array[j] = temp
    j = array.size - 1
    while (i < j) {
        temp = array[i]
        array[i] = array[j]
        array[j] = temp
        i++
        j--
    }
    return true
}
