import java.util.*

fun main(args: Array<String>) {
    val scn = Scanner(System.`in`)
    val N = scn.nextInt()

    var P = ""
    var Q = ""

    for(i in 1..N){
        P += scn.nextInt().toString()
    }
    for(j in 1..N){
        Q += scn.nextInt().toString()
    }

    var ele = ""
    for(i in 1..N){
        ele += i.toString()
    }
    var allList = mutableListOf<Int>()

    permutation(ele, "", allList)
    allList.sort()

    var a = 0
    var b = 0
    var cnt = 1
    for(e in allList){
        if(e==P.toInt()) a = cnt
        if(e==Q.toInt()) b = cnt

        if(a!=0 && b!= 0) break

        cnt++
    }

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

fun permutation(q: String, ans: String, ret:MutableList<Int>) { // Base Case
    if (q.length <= 1) {
        ret.add( (ans+q).toInt() )
    } else {
        for (i in 0 until q.length) {
            permutation(
                q.substring(0, i) + q.substring(i + 1),
                ans + q[i],
                ret
            )
        }
    }
}