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 = permutations((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 permutations(base: String, add: String = "", ret: MutableList<Int> = mutableListOf()): List<Int> {
    if (base.length == 1) {
        ret.add((add + base).toInt())
    } else {
        for (i in base.indices) {
            permutations(
                base.substring(0, i) + base.substring(i + 1, base.length),
                add + base[i],
                ret
            )
        }
    }
    return ret
}
