function main(input){
  let [a, b, c, k] = input.split(' ').map(Number);
  let sum = 0;
  for(let i=0; i<=k; i++){
    if(a <= k){
      sum += 1;
    }else if(a + b <= k){
    }else{
      sum += -1;
    }
  }
  
  console.log(sum);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8').trim());