import Control.Monad
import Data.List hiding (peermutations)

-- main = print =<< (pure (ans . (\[_, b] -> b))) <*> (replicateM 2 getLine)
main = print =<< ((pure getIndex) <*> getInput)

getInts :: IO [Int]
getInts = (pure ((map read) . words)) <*> getLine

getInt :: IO Int
getInt = (pure read) <*> getLine

getInput :: IO [[Int]]
getInput = replicateM 3 getInts

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

permutations :: [Int] -> [[Int]]
permutations [] = [[]]
permutations xs = [(y:zs) | (y, ys) <- select xs, zs <- Main.permutations ys]

num2List :: Int -> [[Int]]
num2List n = Main.permutations [1..n]

getIndex :: [[Int]] -> Int
getIndex [[n], a, b] = (\[c, d]-> abs $ c - d) $ (\p -> map (\m -> (\(Just j) -> j) $ elemIndex m p) [a, b]) $ num2List n
