main = do
    [a, b, c, k] <- map read . words <$> getLine :: IO [Int]
    let answer =
            if k <= a
                then k
                else if k <= a + b
                    then a
                    else a - (k - a - b)
    print $ answer
