"use strict";
var input = require("fs").readFileSync("/dev/stdin", "utf8");
var cin = input.trim().split(/ |\n/), cid = 0;

function main() {
    let A = +cin.shift();
    let B = +cin.shift();
    let C = +cin.shift();
    let K = +cin.shift();
    if (K < A) {
        console.log(K);
    } else if (K <= A + B) {
        console.log(A);
    } else {
        console.log(A - K + (A + B));
    }
}
main();
