import java.util.Scanner

object Main extends App {
  def param(n:Int):List[Int] = if (n > 0) sc.nextInt::param(n-1) else Nil
  val sc = new Scanner(System.in)
  val v = param(4)
  v match {
    case List(a,b,c,k) if a > k    => println(k)
    case List(a,b,c,k) if a+b > k  => println(a)
    case List(a,b,c,k) if a+b <= k => println(a - (k-a-b))
  }
}
