fun main(args: Array<String>) {
    val N = readLine()!!.toInt()
    val P = readLine()!!.split(" ").map(String::toInt).toMutableList()
    val Q = readLine()!!.split(" ").map(String::toInt).toMutableList()
    val score1 = calc(P, N)
    val score2 = calc(Q, N)
    println(Math.abs(score1 - score2))
}

fun calc(a: MutableList<Int>, N: Int): Int {
    var score = 1
    for (i in 0 until N) {
        score += (a[i] - 1) * fact(N - i - 1)
        for (j in i+1 until N) {
            if (a[i] < a[j]) a[j]--
        }
    }
    return score
}

fun fact(d: Int): Int {
    var a = 1
    for (i in 1..d) a *= i
    return a
}