fun main(args : Array<String>) {
    val (a, b, c, k) = readLine()!!.split(" ").map { it.toLong() }
    val cardSum = a + b + c

    if (cardSum == 0L) {
        println(0)
        return
    }

    if (a >= k) {
        println(k)
    }
    else if (a + b >= k) {
        println(a)
    }
    else {
        println(a - (k - a - b))
    }
}