function B167(_input) {
    var lines = _input.split("\n");
    var first = lines.shift().split(' ').map(v => v*1);
    var sum = 0;

    if (first[0] >= first[3]) {
        console.log(first[3]);
    } else {
        sum = first[0];
        first[3] -= first[0];

        if (first[1] >= first[3]) {
            console.log(sum);
        } else {
            first[3] -= first[1];
            sum -= first[3];
            console.log(sum);
        }
    }
}
B167(require("fs").readFileSync("/dev/stdin", "utf8"));