

fun main(arr:Array<String>) {
    val num = readLine()!!.toInt()
    val p = readLine()!!.replace(" ", "")
    val q = readLine()!!.replace(" ", "")

    val list = getJunretsu(0, num)
    val ans = Math.abs(list.indexOf(p) - list.indexOf(q))
    println(ans)
}

fun getJunretsu(flag:Int, total:Int):MutableList<String> {
    if(flag == (1 shl total) - 1) {
        return mutableListOf("")
    }
    val ans = mutableListOf<String>()
    for(i in (0 until total)) {
        val key = 1 shl i
        if(flag.and(key) > 0) {
            continue
        }
        val ret = getJunretsu(flag + key, total)
        ans.addAll(ret.map { (i+1).toString() + it })
    }
    return ans
}


