
import Control.Applicative

main :: IO ()
main = do
    ls <- map read . words <$> getLine
    let [a, b, c, k] = ls

    if k <= a 
        then print k
        else if k - a <= b
            then print a
            else print (a - (k - a - b))
            