import qualified Data.Map as M

select :: [a] -> [(a, [a])]
select [x]    = [(x, [])]
select (x:xs) = (x, xs) : map (\(y, ys) -> (y, x:ys)) (select xs)

permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations xs =
  concatMap (\(y, ys) -> map (y:) (permutations ys)) $ select xs

main = do
 n <- read <$> getLine :: IO Int 
 let permutationsList = M.fromList $ zip (permutations [1..n]) [1..]
 pList <- map read . words <$> getLine :: IO[Int]
 qList <- map read . words <$> getLine :: IO[Int] 
 print $ abs $ (permutationsList M.! pList) - (permutationsList M.! qList)