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

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