function Main(input) {
  input = input.split(" ");
  const A = parseInt(input[0], 10);
  const B = parseInt(input[1], 10);
  const C = parseInt(input[2], 10);
  var K = parseInt(input[3], 10);

  var count = 0;
  if (A < K) {
    count += A;
    K -= A;
  } else {
    console.log(K);
    return;
  }

  if (B < K) {
    K -= B;
    count -= K;
    console.log(count);
    return;
  } else {
    console.log(A);
    return;
  }
}

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