import Data.IORef
import Control.Monad

main :: IO()
main = do
        n <- readLn
        x <- readLn
        y <- readLn
        print =<< solve (y - x) (min x y) n

solve :: Int -> Int -> Int -> IO Int
solve a b n = do
                a' <- newIORef a
                b' <- newIORef b
                solve' a' b' n 2
    where solve' x' y' n m | n == m    = readIORef x'
                           | otherwise = do
                              z <- readLn
                              x <- readIORef x'
                              y <- readIORef y'
                              when (z - y > x) $ writeIORef x' (z - y)
                              when (z < y) $ writeIORef y' z
                              solve' x' y' n (m + 1)