#include<stdio.h>
void main()
{
 unsigned long a,b,c,k;
  long i;
   scanf("%lu%lu%lu%lu"&a,&b,&c,&k);
  if(k<=a){
    printf("%lu",k);}
  else if(k<=a+b){
    printf("%lu",a);}
  else{
    c=k-(a+b);
    i=a-c;
    printf("%ld",i);}
}