addNumCard :: Integer -> Integer -> Integer -> Integer -> Integer
addNumCard a b c d | d <= 0 = 0
                   | a > 0 = (min a d) + addNumCard 0 b c (d -a)
                   | b > 0 = 0 + addNumCard a 0 c (d -b)
                   | c > 0 = (-1 * (min c d)) + addNumCard a b (c-1) (d -c)
                   | otherwise = 0

main = do
    [a,b,c,d] <- map read <$> (words <$> getLine) :: IO [Integer]
    putStrLn $ show  $ addNumCard a b c d
    