solve :: Int -> Int -> Int -> Int -> Int
solve a b c k
 | k <= a = k
 | k <= b = a
 | otherwise = a - (k - a - b)

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