"use strict";

const main = arg => {
  const inputList = arg.trim().split("\n");
  // 回答を記述
  const A = BigInt(inputList[0].split(" ")[0]);
  const B = BigInt(inputList[0].split(" ")[1]);
  const C = BigInt(inputList[0].split(" ")[2]);
  const K = BigInt(inputList[0].split(" ")[3]);

  if (K <= (A + B)) {
    console.log(BigInt.asIntN(64, A).toString())
  } else {
    // A + B <= K <= A + B + C
    const minus = K - (A + B);
    console.log(BigInt.asIntN(64,(A - minus)).toString())
  } 

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