fun main(args: Array<String>) {
    val (A, B, C, K) = readLine()!!.split(" ").map { it.toLong() }

//    fun calcElems(elemNums: List<Long>, amount: Long, calcResult: List<Long> = listOf(), index: Int = 1): List<Long> {
//        val targetNum = elemNums.first()
//        val (newAmount, newElemNum) = if (amount > targetNum) {
//            Pair(amount - targetNum, targetNum)
//        } else {
//            Pair(0L, amount)
//        }
//        val newResult = calcResult.plusElement(newElemNum)
//
//        return if (elemNums.size == index) newResult else calcElems(elemNums, newAmount, newResult, index + 1)
//    }
//    val (a, _, c) = calcElems(listOf(A, B, C), K)
    var amount = K
    val a = if (amount > A) {
        amount -= A
        A
    } else {
        val tmp = amount
        amount = 0
        tmp
    }
    
    val b = if (amount > B) {
        amount -= B
        B
    } else {
        val tmp = amount
        amount = 0
        tmp
    }
    
    val c = if (amount > C) {
        amount -= C
        C
    } else {
        amount
    }
    
    println(a - c)
}

