import Control.Applicative

main :: IO ()
main = solve <$> f >>= print
  where
    f = map read <$> words <$> getLine

solve :: [Int] -> Int
solve [a, b, c, k]
  | a >= k = k
  | a + b >= k = a
  | otherwise = 2 * a + b - k
