import kotlin.math.*
import java.util.*

fun main(args: Array<String>) {
  var (a, b, c, k) = readLine()!!.split(' ').map(String::toInt)
  var x = min(k, a)
  k -= a + b
  if (0 < k) {
    x -= min(k, c)
  }
  println(x)
}
