function Main(input) {
    // 1行目がinput[0], 2行目がinput[1], …に入る
    input = input.trim();
    var A = input.split(' ')[0];
    A = parseInt(A);
    var B = input.split(' ')[1];
    B = parseInt(B);
    var C = input.split(' ')[2];
    C = parseInt(C);
    var K = input.split(' ')[3];
    K = parseInt(K);
    var count = 0;
    Number(count);
    if (K >= A) {
        count = count + A;
        K = K - A;
    } else {
        count = count + A - (A - K);
        K = K - A;
    }
    if (K - B > 0) {
        K = K - B;
        if (K >= C) {
            count = count - C;
        } else {
            count = count - C + (C - K);
        }
    }
    console.log(count);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));