function Main(input) {
  const inputArr = input.split(" ");

  const a = parseInt(inputArr[0], 10);
  const b = parseInt(inputArr[1], 10);
  const c = parseInt(inputArr[2], 10);
  const k = parseInt(inputArr[3], 10);

  let numArr = [];

  for (let i = 0; i < a; i++) {
    numArr.push(1);
  }
  for (let i = 0; i < b; i++) {
    numArr.push(0);
  }
  for (let i = 0; i < c; i++) {
    numArr.push(-1);
  }

  let total = 0;

  for (let i = 0; i < k; i++) {
    total += numArr[i];
  }

  console.log(total);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));
