import java.util.*

fun main(args: Array<String>) {
    class Permutation<T> private constructor(
        private val baseIndex: Int,
        private var index: Int,
        val target: Array<T>?
    ) {
        private var subPermutation: Permutation<T>? = null

        constructor(objs: Array<T>) : this(0, 0, objs.clone())

        init {
            if (target == null || target.isEmpty()) {
                throw IllegalArgumentException("")
            }

            if (this.index < this.target.size - 1) {
                this.subPermutation = Permutation(this.baseIndex + 1, this.index + 1, this.target)
            }
        }

        operator fun next(): Boolean {
            if (this.subPermutation == null) {
                return false
            }
            val result = this.subPermutation!!.next()
            if (result) {
                return true
            }
            this.swap(this.baseIndex, this.index)
            ++this.index
            if (this.target!!.size <= this.index) {
                this.index = this.baseIndex
                return false
            }
            this.swap(this.index, this.baseIndex)
            return true


        }

        private fun swap(index1: Int, index2: Int) {
            val tmp = this.target!![index1]
            this.target[index1] = this.target[index2]
            this.target[index2] = tmp
        }
    }

    val N = readLine()!!.toInt()

    val PN = readLine()!!.split(" ").map(String::toInt).toTypedArray()
    val QN = readLine()!!.split(" ").map(String::toInt).toTypedArray()

    val permutation = Permutation((1..N).toList().toTypedArray())

    var a = 0
    var b = 0
    val list = mutableListOf<String>()
    do {
        list.add(permutation.target!!.joinToString(""))
    } while (permutation.next())
    list.sort()

    for ((i, s) in list.withIndex()) {
        val index = i + 1
        val array = s.toCharArray().map { it.toString().toInt() }.toTypedArray()
        if (Arrays.deepEquals(PN, array)) {
            a = index
        }
        if (Arrays.deepEquals(QN, array)) {
            b = index
        }

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

    println(Math.abs(a - b))

}

