"use strict";

const main = arg => {
    const input = arg.trim().split(" ");

    let a = parseInt(input[0]);
    let b = parseInt(input[1]);
    let c = parseInt(input[2]);
    let k = parseInt(input[3]);

    let sum = 0

    if(a >= k){
      	sum = k
        console.log(sum)
    } else {
        k = k - a
        sum = a
        if(b >= k){
            console.log(sum)
        } else {
            k = k - b
            console.log(sum - k)
        }
    }
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));  