import java.io.{BufferedOutputStream, BufferedReader, InputStreamReader, PrintWriter}

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val out = new PrintWriter(new BufferedOutputStream(System.out))
    val Array(a, b, c, k) = in.readLine().split("\\s+").map(_.toInt)

    out.println(
      if (k < a) {
        k
      } else if (k < a + b) {
        a
      } else if (k < a + b + c) {
        a - (k - a - b)
      } else {
        a - c
      }
    )
    out.flush()
  }
}
