function Main(input){
  input = input.trim().split(' ').map(x=>parseInt(x));
  var a = input[0];
  var b = input[1];
  var c = input[2];
  var k = input[3];
  var ans = 0;
  if (a>k){
    ans = k;
  }else if (a+b>k){
    ans = a;
  }else{
    ans = a - (k-a-b);
  }
  console.log(ans);
}


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