object Main {
  def main(args: Array[String]): Unit = {
    import scala.io.StdIn.readLine
    val Array(datA, datB, datC, datK) = readLine.split(" ").map(_.toLong)

    val ans =
    if(datK <= datA) datK
    else if(datK <= datA + datB) datA
    else datA - (datK - (datA + datB))

    println(ans)
  }
}
