import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val a = sc.nextInt()
  val b = sc.nextInt()
  val c = sc.nextInt()
  val k = sc.nextInt()

  println(abc167b(a, b, c, k))
}

private fun abc167b(a: Int, b: Int, c: Int, k: Int): Int {
  val ka = k - a
  if (ka <= 0) return k
  val kab = ka - b
  if (kab <= 0) return a

  return a - kab
}
