object Main extends App{
    val Array(a,b,c,k) = scala.io.StdIn.readLine.split(" ").map(_.toInt)
    if(1 <= k && k <= a)          println(k)
    if(a < k && k <= a+b)        println(a)
    if(a+b < k && k <= a+b+c)    println(a-(k-(a+b)))
    
}
