import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val A, B, C, K = sc.nextInt

  val x =
    if (A >= K) K
    else if (A + B >= K) A
    else A - 1 * (K - A - B)
  println(x)
}