'use strict'

function main(input) {
    let a = Number(input.split('\n')[0].split(' ')[0]);
    let b = Number(input.split('\n')[0].split(' ')[1]);
    let c = Number(input.split('\n')[0].split(' ')[2]);
    let d = Number(input.split('\n')[0].split(' ')[3]);

    let sum = 0;
    let tmp;
    if (d-a>=0) {
        sum+=a;
        tmp=d-a;
        if (tmp-b>=0) {
            tmp=tmp-b;
            if (tmp-c>=0) {
                sum-=c;
            } else {
                sum-=tmp;
                console.log(sum);
                return;
            }
        }
    }
    console.log(sum);
}

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