import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  [a, b, c, k] <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    m :: Int
    m | k <= a + b = min a k
      | otherwise  = a - (k - a - b)
  print m

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
{-# INLINE unsafeTextToInt #-}
