{-# LANGUAGE RankNTypes #-}
import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad.State (StateT, evalStateT, get, gets, modify, lift)
import Control.Monad.ST
import Data.STRef
import Data.Map.Strict (Map, lookup, insert, empty)
import Data.Maybe (maybe)
import Data.Array as A

main :: IO ()
main = breads >>= main'
    where
        main' [n, z, w] = breads >>= print . solve n w

solve :: Int -> Int -> [Int] -> Int 
solve n w xs = run calc (True, 0, w)
    where
        arr = A.listArray (0, n - 1) xs
        calc f (b, idx, w)
            | idx < n - 1 = cmpf b <$> f (b, idx + 1, w) <*> f (not b, idx + 1, arr A.! idx)
            | otherwise = return . abs $ w - arr A.! idx 

        cmpf b 
            | b = max
            | otherwise = min

type Memoizer s a b = a -> StateT (STRef s (Map a b)) (ST s) b

run :: (Ord a) => (forall s . Memoizer s a b -> Memoizer s a b) -> a -> b
run f k = runST $ newSTRef empty >>= evalStateT (memoizeFix f k)

memoizeFix :: (Ord a) => (Memoizer s a b -> Memoizer s a b) -> Memoizer s a b
memoizeFix f k = do
    mc <- get
    c <- lift $ readSTRef mc 
    case Data.Map.Strict.lookup k c of
        Just v -> return v
        _ -> do
            newv <- f (memoizeFix f) k
            lift $ modifySTRef' mc (Data.Map.Strict.insert k newv)
            return newv



breads :: IO [Int]
breads = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)

