#include<stdio.h>
int main(void){
  int a,b,c,k;
  scanf("%d %d %d %d",&a,&b,&c,&k);
  if(a>=0&&b>=0&&c>=0&&1<=k<=a+b+c<=2000000000){
int z=k-a-b;
int sum=0;;
if(k>=a){
  sum=sum+(a*1);
}
//printf("%d\n",sum);
if(z<c){
  sum=sum+(z*-1);
  //printf("%d\n",z*-1);
}
printf("%d\n",sum);
}
return 0;
}
