program main
  implicit none
  integer(8) :: a, b, c, k, filled, fill, res
  read *, a, b, c, k

  fill = min(a, k)
  res = fill
  filled = fill

  fill = min(b, k - filled)
  filled = filled + fill

  fill = min(c, k - filled)
  res = res - fill
  filled = filled + fill

  print "(i0)", res
end program main
