import Data.List
import Data.Char
import Data.Maybe
import Control.Monad
import qualified Data.ByteString.Char8 as BS8

main :: IO ()
main = do
    n <- fst . fromJust . BS8.readInt <$> BS8.getLine
    p <- unfoldr (BS8.readInt . BS8.dropWhile isSpace) <$> BS8.getLine
    q <- unfoldr (BS8.readInt . BS8.dropWhile isSpace) <$> BS8.getLine
    
    let l = sort $ permutations [1..n]

    print $ abs $ (fromJust $ elemIndex p l) - (fromJust $ elemIndex q l)
