object Main extends App {
  import java.util.Scanner
  val sc = new Scanner(System.in)

  val a = sc.nextInt()
  val b = sc.nextInt()
  val c = sc.nextInt()
  val k = sc.nextInt()

  val result = if (k <= a) {
    k
  } else if (k <= (a + b)) {
    a
  } else {
    a - (k - a - b)
  }
  println(result)
}
