import java.lang.Math

fun main(args: Array<String>) {
    val N = readInputLine().toInt()
    val P = readInputLine().split(" ").map { it.toInt() }.toIntArray()
    val Q = readInputLine().split(" ").map { it.toInt() }.toIntArray()

    val perm = IntArray(N) { it + 1 }

    var cnt = 1
    var a = 0
    var b = 0

    do {
        var ok = true
        for (i in 0 until N) {
            if (P[i] != perm[i]) {
                ok = false
                break
            }
        }
        if (ok) a = cnt
        ok = true
        for (i in 0 until N) {
            if (Q[i] != perm[i]) {
                ok = false
                break
            }
        }
        if (ok) b = cnt
        cnt++
    } while (nextPermutation(perm))

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

fun readInputLine(): String {
    return readLine()!!
}

fun nextPermutation(arr: IntArray): Boolean {
    var i = arr.size - 1
    while (i > 0 && arr[i - 1] >= arr[i]) {
        i--
    }

    if (i <= 0) {
        return false
    }

    var j = arr.size - 1
    while (arr[j] <= arr[i - 1]) {
        j--
    }

    var temp = arr[i - 1];
    arr[i - 1] = arr[j];
    arr[j] = temp;

    j = arr.size - 1;
    while (i < j) {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        i++;
        j--;
    }

    return true;
}
