fun main(args: Array<String>) {
    fun nextPermutation(arr: IntArray): Boolean {
        var i = arr.size - 1
        while (i > 0 && arr[i - 1] >= arr[i]) {
            i--
        }

        if (i <= 0) {
            return false
        }

        var j = arr.size - 1
        while (arr[j] <= arr[i - 1]) {
            j--
        }

        var temp = arr[i - 1]
        arr[i - 1] = arr[j]
        arr[j] = temp

        j = arr.size - 1
        while (i < j) {
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp
            i++
            j--
        }

        return true
    }

    val N = readLine()!!.toInt()

    val PN = readLine()!!.split(" ").map(String::toInt).toIntArray()
    val QN = readLine()!!.split(" ").map(String::toInt).toIntArray()

    val array = (1..N).toList().toIntArray()

    var index = 1
    var a = 0
    var b = 0
    do {
        if (PN.joinToString("") == array.joinToString("")) {
            a = index
        }
        if (QN.joinToString("") == array.joinToString("")) {
            b = index
        }
        index++

    } while (nextPermutation(array))

    println(Math.abs(a - b))
}

