process.stdin.resume();
process.stdin.setEncoding('utf8');
var lines = [];
var reader = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
reader.on('line', (line) => {
    lines.push(line);
});
reader.on('close', () => {
    const [a, b, c, k] = lines[0].split(" ").map(e => parseInt(e));
    //lines.shift();
    const t = k - (a + b);
    console.log( t <= 0 ? a : (a - t));
});