import qualified Data.Set as S
main=do
 n<-readLn
 p<-words<$>getLine
 q<-words<$>getLine
 print(abs((f n n (S.fromList p) p 0)-(f n n (S.fromList q) q 0)))
  where f n m s p i |m==0      =i
                    |otherwise =f n (m-1) (S.delete (head p) s) (tail p) (i*m+(S.findIndex(head p)s))