import java.util.Scanner

import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks
import scala.math.abs

object Main extends App {

  val sc = new Scanner(System.in)
  val a, b, c, k = sc.nextInt()

  val res = (if (a >= k) {
    a
  } else if (a + b >= k) {
    a
  } else {
    a * 1 + ((k - a - b) * -1)
  })

  println(res)
}
