"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    let A = parseInt(arg[0].split(" ")[0]);
    let B = parseInt(arg[0].split(" ")[1]);
    let C = parseInt(arg[0].split(" ")[2]);
    const K = parseInt(arg[0].split(" ")[3]);
    
    // 1 0 -1の順で優先してとる
    const one = A >= K ? K : A;
    
    const zero = Math.max(K - A, 0);
    
    const minusOne = Math.max(K - (A + B), 0);
    
    console.log((1 * one) + (0 * zero) + (-1 * minusOne));
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));