{-# LANGUAGE FlexibleContexts #-}
import           Control.Monad
import           Control.Monad.ST
import           Data.Array.IO
import           Data.Bits
import qualified Data.ByteString.Char8       as BS
import           Data.Char
import           Data.Maybe
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM


readInt = fst . fromJust . BS.readInt
getInt = readInt <$> BS.getLine
getIntVec n = V.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

main = do
  n <- getInt
  p <- getIntVec n
  q <- getIntVec n
  print $ if p < q then solve p q 0 else solve q p 0
  where
    solve p q m | p == q = m
                | otherwise = solve p'' q (m + 1)
                  where p'' = runST $ do
                        p' <- V.thaw p
                        VM.nextPermutation p'
                        V.freeze p'