function main(arg) {
  const array = arg.trim().split(" ");
  const A = parseInt(array[0], 10);
  const B = parseInt(array[1], 10);
  const C = parseInt(array[2], 10);
  const K = parseInt(array[3], 10);

  let result = 0;
  result = K - A;
  if (result <= 0) {
    console.log(K);
    return;
  }

  result = result - B;
  if (result <= 0) {
    console.log(K - B);
    return;
  }

  console.log(K - C - result);
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
// main("2000000000 0 0 2000000000");
//https://atcoder.jp/contests/abc167/tasks/abc167_b
