import kotlin.math.min

fun main() {
    var (a,b,c,k) = readLine()!!.split(" ").map { it.toInt() }
    val l = arrayOf(a,b,c)
    val arr = arrayOf(1,0,-1)
    var ans = 0L
    for(i in 0 until 3) {
        val num = min(k, l[i])
        ans += num * arr[i]
        k -= min(l[i], k)
        if(k <= 0)
            break
    }
    print(ans)
}