import Data.List
main = do
 n<- readLn
 s <- words<$>getLine
 t <- words<$>getLine
 let u = sort(map concat (permutations (map show [1..n])))
 print$abs(head(elemIndices (concat s) u) - head(elemIndices (concat t) u))