function Main(input){
  input = input.split(" ").map(Number);
  if(input[3]<=input[0]+input[1]){
      console.log(input[0]);
  }else{
      console.log(input[0]-(input[3]-input[0]-input[1]));
  }
  
}Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());