import qualified Data.ByteString.Char8 as BS
import qualified Data.List as L
import qualified Data.Set as S
import qualified Control.Monad as M
import Data.Maybe (fromJust)
import Data.Ord
import qualified Data.Vector.Unboxed as V



main = do

 [a,b,c,k] <- map (fst.fromJust.BS.readInt) <$>BS.words<$>BS.getLine :: IO [Int]
 print $ if a>k
  then k
  else
   if b>(k-a)
    then a
    else a-(k-a-b)