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

    val array = mutableListOf<Int>()
    repeat(n) {
        array.add(it+1)
    }

    var pIndex = -1
    var qIndex = -1
    var pos = 0

    do {
        pos++
        var pFailed = false
        var qFailed = false

        array.forEachIndexed { index, i ->
            if (i != pp[index]) pFailed = true
            if (i != qq[index]) qFailed = true
            if (pFailed && qFailed) {
                return@forEachIndexed
            }
        }

        if (!pFailed) pIndex = pos
        if (!qFailed) qIndex = pos

        if (pIndex != -1 && qIndex != -1) {
            println(Math.abs(pIndex - qIndex))
            return
        }
    } while (nextPermutation(array))
}

fun nextPermutation(array: MutableList<Int>): Boolean {
    var tmp = 0
    for (i in array.size-2 downTo 0) {
        if (array[i] < array[i+1]) {
            for (j in array.size-1 downTo i+1) {
                if (array[j] > array[i]) {
                    tmp = array[i]
                    array[i] = array[j]
                    array[j] = tmp

                    val tmpArray = array.takeLast(array.size-(i+1)).sorted()
                    tmpArray.forEachIndexed { index, e ->
                        array[i+1+index] = e
                    }

                    return true
                }
            }
        }
    }

    return false
}