fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val p = readLine()!!.split(" ").map { it.toInt() }
    val q = readLine()!!.split(" ").map { it.toInt() }

    val plist = mutableListOf<Int>()
    val qlist = mutableListOf<Int>()
    
    for(i in 1..n) {
        plist.add(i)
        qlist.add(i)
    }
    

    val prank = mutableListOf<Int>()
    val qrank = mutableListOf<Int>()
    
    for (i in 0 until n) {
        val pi = plist.indexOf(p[i])
        val qi = qlist.indexOf(q[i])
        prank.add(pi)
        qrank.add(qi)
        plist.removeAt(pi)
        qlist.removeAt(qi)
    }
    
    var k = 1
    var result = 0
    for (i in 2..n) {
        val index = n - i
        result += (prank[index] - qrank[index])*k
        k *= i
    }
    if (result < 0) {
        result *= -1
    }
    print(result)
}