"use strict";

const main = (input) => {
    const spi = input.split('\n');
    let [a,b,c,k] = spi[0].split(' ').map(e => Number(e));

    let ans = 0;

    const ak = Math.min(a, k);
    ans = ak;
    k -= Math.min(a, ak);

    if (k > 0)
        k -= Math.min(b, Math.min(b, k));

    if (k > 0) {
        const ck = Math.min(c, k);
        ans -= ck;
    }

    console.log(ans);
};

main(require("fs").readFileSync("/dev/stdin", "utf8"));
