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);
  let K = parseInt(array[3], 10);

  let answer = 0;

  if (K >= A) {
    answer += A;
    K -= A + B;
    console.log(K > 0 ? answer - K : answer);
  } else {
    console.log(answer + K);
  }
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
// main("2 1 1 3");
// main("1 2 3 4");
// main("0 0 2000000000 1000000000");
// main("0 0 20 10");
// main("2000000000 0 0 2000000000");
//https://atcoder.jp/contests/abc167/tasks/abc167_b
