open Scanf
open Printf

let easy_linear a b c k =
  if k <= a then k
  else if k <= a + b then a
  else if k <= a + b + c then a - (k - (a + b))
  else a - c

let () = scanf "%d %d %d %d" easy_linear |> printf "%d\n"