module Main where

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

main :: IO ()
main = do
    [a,b,c,k] <- map (fst.fromJust.BS.readInt).BS.words <$> BS.getLine
    let one = if k < a then k else a 
    let zero = if k-one < b then k-one else b
    let minus = k - one -minus
    putStr $ show (one-minus)