import java.util.*

fun <T> permutationList(sortedList: Array<T>): List<List<T>> {
    val permutations = mutableListOf<List<T>>()
    val queue = ArrayDeque<List<T>>().apply { addLast(listOf()) }
    while (queue.isNotEmpty()) {
        val p = queue.removeLast()

        if (p.size == sortedList.size) {
            permutations.add(p)
        } else {
            sortedList.filterNot { p.contains(it) }
                .forEach { queue.addLast(p + it) }
        }
    }

    return permutations
}

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

    val per = permutationList((1..n).toList().toTypedArray())
    Math.abs(per.indexOf(p) - per.indexOf(q)).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)
