import scala.io.StdIn._

object Main extends App {
  val Array(a, b, c, k) = readLine.split(" ").map(_.toInt)

  val (aCount, k1) = if (k >= a) (a, k - a) else (k, 0)
  val (_, k2) = if (k1 >= b) (b, k1 - b) else (k1, 0)
  val cCount = if (k2 >= c) c else k2

  println(aCount - cCount)
}

