{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Data.Array.Unboxed
import Data.Array.ST
import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Data.List
import Data.Char

main :: IO ()
main = do
    [a,b,c,k] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
    print $ if k <= a then k 
        else if k <= a+b then a
        else a-(k-c)