import Data.Maybe
import qualified Data.ByteString.Char8 as BS

readInt = fst . fromJust . BS.readInteger
readIntList = map readInt . BS.words
getIntList = readIntList <$> BS.getLine

main = do
  [a, b, c, k] <- getIntList
  if a > k then
  	putStrLn $ show k
  else
    if b > (k - a) then
      putStrLn $ show a
    else
      putStrLn $ show (2 * a - k + b)