const input = require('fs').readFileSync('/dev/stdin', 'utf8').split('\n');
const line = input[0].split(' ')
const A = +line[0]
const B = +line[1]
const C = +line[2]
const K = +line[3]

// console.log(`A=${A}`)
// console.log(`B=${B}`)
// console.log(`C=${C}`)
// console.log(`K=${K}`)

if (K <= A) {
    // console.log('A <= K')
    console.log(K)
} else if (K <= A + B) {
    // console.log('A + B <= K')
    console.log(A)
} else {
    const effectiveC = K - (A + B);
    console.log(A - effectiveC)
}
