fun main(args: Array<String>) {
  var (a, b, c, k) = readLine()!!.split(" ").map{ it.toInt() }
  var count = 0
  for (n in 1..k) {
    if(0 < a) {
      count++
      a -= 1
    } else if (a == 0 && b == 0) {
      count--
      c -= 1
    } else {
      b -= 1
    }
  }
  println(count.toString())
}