"use strict"
const assert = require('assert');

function main(input) {
    let ret = check(input);
    console.log(ret);
}

function check(input) {
    let ans = 0;
    let [a, b, c, k] = input.split(" ").map((i) => parseInt(i));
    if (a >= k && k != 0) {
        ans += k;
        k = 0;
    } else if (a < k) {
        ans += a;
        k -= a;
    }
    if (b >= k && k != 0) {
        k = 0;
    } else if (b < k) {
        k -= b;
    }
    if (c >= k && k != 0) {
        ans -= k;
        k = 0;
    } else if (c < k) {
        ans -= c;
        k -= c;
    }
    return ans;
}

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

// check
// var answer;

// input = `2 1 1 3`;
// answer = `2`;
// assert.equal(check(input), answer);

// input = `1 2 3 4`;
// answer = `0`;
// assert.equal(check(input), answer);

// input = `2000000000 0 0 2000000000`;
// answer = `2000000000`;
// assert.equal(check(input), answer);