import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

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

main = do
    [a,b,c,k] <- getIntList
    let ans | a >= k = k
            | a+b >= k = a
            | otherwise = a - (k-a-b)
    print ans