import Data.List

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

solve :: Int -> Int -> Int -> Int -> Int
solve 0 0 _ k = -k
solve 0 b c k = solve 0 0 c (k - b)
solve a b c k
  | a > k     = k
  | otherwise = a + solve 0 b c (k - a)
