import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val a,b,c,k = sc.nextInt

  val res: Long = if (a+b >= k) {
    a
  } else {
    a - (k-(a+b))
  }
  println(res)
}