fun main() {
    val (a, b, c, k) = readLine()?.split(" ")?.map(String::toInt) ?: return
    if (k <= a) {
        println(k.toString())
        return
    }
    val r = k - a
    if (r - b > 0) {
        println((a - (r - b)).toString())
    } else {
        println(a.toString())
    }
}