#include<stdio.h>

void main(void)
{
  long int a,b,c,k;
  long int result=0;
  scanf("%ld %ld %ld %ld",&a,&b,&c,&k);
  while(k>0){
    if(a-->0){
      result+=1;
    }else if(b-->0){
      ;
    }else{
      result-=1;
    }
    k--;
  }
  if(result<0){
    result=0;
  }
  printf("%ld\n",result);
}
