inp = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n')
l = inp.shift().split(" ")
A = l.shift() * 1
B = l.shift() * 1
C = l.shift() * 1
K = l.shift() * 1

if (K - A < 0) {
    console.log(K)
} else {
    k = K - A;
    if (k - B < 0) {
        console.log(A)
    } else {
        k = k - B
        if (k - C < 0) {
            console.log(A - k)
        } else {
            console.log(A - C)
        }
    }
}
