fun main(args: Array<String>) {
    abc150c()
}

fun abc150c() {
    val n = readLine()!!.toInt()
    val p = readLine()!!.split(' ').map { it.toInt() }.joinToString("")
    val q = readLine()!!.split(' ').map { it.toInt() }.joinToString("")

    val perms = permutation((1..n).joinToString(""), "").map { it.toString() }

    val a = perms.indexOf(p)
    val b = perms.indexOf(q)

    val answer = Math.abs(a - b)

    println(answer)
}

fun permutation(q: String, ans: String): List<Int> {
    return if (q.length <= 1) {
        listOf((ans + q).toInt())
    } else {
        (q.indices).map {
            permutation(q.substring(0, it) + q.substring(it + 1), ans + q[it])
        }.flatten()
    }
}
