#include<stdio.h>
int main(void){
  long a,b,c,k;
  scanf("%ld%ld%ld%ld",&a,&b,&c,&k);
  if(k<=a)printf("%ld",k);
  else if(k<=a+b)printf("%ld",a);
  else if(k<=a+b+c)printf("%ld",2*a-k+b);
  else printf("%ld",a-c);
  return 0;
}