function main(lines) {
    const input = lines.split(' ').map(Number)
    const A = input[0]
    const B = input[1]
    const C = input[2]
    const K = input[3]

    var variableNumber = K
    var sum = 0
    
    if(K-A>0){
        variableNumber -= A
        sum += A
    } else if(K-A<=0){
        variableNumber = 0
        sum += K
    }

    if(variableNumber - B>0){
        variableNumber -= B
    } else if(variableNumber - B<=0){
        variableNumber = 0
    }

    if(variableNumber - C <= 0){
        sum -= variableNumber
        console.log(sum)
    }
        
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));

