import kotlin.math.abs

fun main() {
    class Permutation<T:Comparable<T>> (private var li: List<T>){

        val size_li:Int
        val size_e:Int
        val sortedList: List<List<T>>
        val invMap: Map<List<T>, Int>
        private var is_setting:Boolean =false
        var now: List<T>
            set(value){
                if (is_setting){
                    field=value
                }else{
                    this.setting(value = value)
                }
            }
        var ind:Int
            set(value){
                if (is_setting){
                    field=value
                }else{
                    this.setting(index = value)
                }

            }

        init {
            this.is_setting=true
            this.now= listOf<T>()
            this.ind=-1
            this.size_li=this.fact(li.size)
            this.size_e=li.size
            this.sortedList=this.getList(li.sorted()).toList()
            this.invMap= this.getMap().toMap()
            this.setting(value = li)
        }

        private fun setting(index:Int?=null, value:List<T>?=null){
            this.is_setting=true
            if (index!==null) {this.ind= index; this.now=this.sortedList[index]}
            if (value!==null) {this.now= value; this.ind= this.invMap[value] ?: error("out of map")}
            this.is_setting=false
        }
        fun next():Boolean{
            this.ind =(this.ind+1)%this.size_li
            this.now=this.sortedList[this.ind]
            return this.ind != 0
        }
        fun prev():Boolean{
            this.ind=(this.ind-1+this.size_li)%this.size_li
            this.now=this.sortedList[this.ind]
            return this.ind!=this.size_li-1
        }
        private fun getList(li_before: List<T>, li_after: List<T> =listOf<T>(), tmp_ans: MutableList<List<T>> =mutableListOf<List<T>>()): MutableList<List<T>>{
            var ans:MutableList<List<T>> =mutableListOf<List<T>>()
            if (li_before.size<=1){
                tmp_ans.add(li_after+li_before)
                ans= tmp_ans
            }else{
                for (i in li_before.indices){
                    ans= this.getList(li_before.take(i)+li_before.takeLast(li_before.size-1-i), li_after+li_before[i],tmp_ans)
                }
            }
            return ans
        }
        private fun getMap():MutableMap<List<T>, Int>{
            var ans= mutableMapOf<List<T>, Int>()
            this.sortedList.forEachIndexed { index, e -> ans[e]=index }
            return ans
        }
        private fun fact(n:Int): Int{
            return if (n==0) 1 else n*this.fact(n-1)
        }
    }
    readLine()
    var p= readLine()!!.split(" ").map { it.toInt() }
    var q= readLine()!!.split(" ").map { it.toInt() }
    var perm=Permutation(p)
    var tmp=perm.ind
    perm.now=q
    println(abs(tmp-perm.ind))

}