import java.util.*

tailrec fun <T> calcPermutationList(
    sortedList: Array<T>,
    queue: ArrayDeque<List<T>>,
    permutationList: MutableList<List<T>> = mutableListOf()
): List<List<T>> {
    if (queue.isEmpty()) {
        return permutationList
    }

    val p = queue.removeFirst()
    if (p.size == sortedList.size) {
        permutationList += p
    } else {
        queue += sortedList.filterNot { p.contains(it) }.map { p + it }
    }

    return calcPermutationList(sortedList, queue, permutationList)
}

fun main(args: Array<String>) {
    val n = readInteger()
    val p = readIntegerList()
    val q = readIntegerList()

    calcPermutationList((1..n).toList().toTypedArray(), ArrayDeque<List<Int>>().apply { addFirst(listOf()) })
        .let { it.indexOf(p) - it.indexOf(q) }
        .let(Math::abs)
        .let(::println)
}

fun readInteger() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntegerList() = readStringList().map(String::toInt)
fun readLongList() = readStringList().map(String::toLong)
