-- {-# LANGUAGE TypeApplications #-}
-- {-# LANGUAGE BlockArguments #-}
import Control.Monad
import Data.Maybe
import Data.Char (isSpace)
import Data.List (unfoldr)
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.ByteString.Char8 as BS

tuplify2 (x:y:_) = (x,y)
tuplify2 _ = undefined

--Input functions with ByteString
readInt = fst . fromJust . BS.readInt
readIntTuple = tuplify2 . map readInt . BS.words
readIntList = map readInt . BS.words

getInt = readInt <$> BS.getLine
getIntN n = map readInt <$> replicateM (fromIntegral n) BS.getLine
getIntList = readIntList <$> BS.getLine
getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine
getIntMatrix = map readIntList . BS.lines <$> BS.getContents
getIntTuple = readIntTuple <$> BS.getLine
getIntNTuples n = map readIntTuple <$> replicateM (fromIntegral n) BS.getLine
getIntTuples = map readIntTuple . BS.lines <$> BS.getContents

permutation :: [a] -> Int -> [[a]]
permutation [] _    = [[]]
permutation ns size = perm size (length ns - 1) [(ns, [])]
    where
      perm 0 _ xs = [a | (_, a) <- xs]
      perm c n xs = perm (c - 1) (n - 1) $ concatMap (f n) xs
      f n (xs, ys) = [(as ++ bs, ys ++ [b]) |
                      x <- [0 .. n], let (as, b : bs) = splitAt x xs]

getIndex x (a:as) = if (snd a) == x
                    then (fst a)
                    else getIndex x as

main = do
  n <- getInt
  ps <- getIntList
  qs <- getIntList

  let ansList = zip [0,1..] (permutation [ 1 .. n ] n)
      a = getIndex ps ansList
      b = getIndex qs ansList
  print $ abs (a - b)