main = do
  li <- getLine
  let [a,b,c,k] = map read $ words li
  let ans = compute a b c k
  print ans

compute :: Int -> Int -> Int -> Int -> Int
compute a b c k
  | a >= k = k
  | b >= k1 = a
  | True = a - k2
  where
    k1 = k - a
    k2 = k1 - b