fun main(args: Array<String>) {
  val (
    oneCount
    , zeroCount
    , minusOneCount
    , chooseCount
  ) = readLine()!!.split(" ").map(String::toInt)
  
  if (oneCount >= chooseCount) {
    println(chooseCount)
  } else {
    // oneCount < chooseCount
    if (oneCount + zeroCount >= chooseCount) {
      println(oneCount)
    } else {
      val minusCount = chooseCount - oneCount - zeroCount
      println(oneCount - minusCount)
    }
  }
}