#include<stdio.h>
#include<string.h>

int main()
{
  long int a,b,c,k,t;
  
  scanf("%ld", &a);
  scanf("%ld", &b);
  scanf("%ld", &c);
  scanf("%ld", &k);
  
  if(k <= a || k <= a + b)
  {
    printf("%ld", k);
  }else 
  {
    t = a - (k - (a + b));
    printf("%ld", t);
  }
  return 0; 
}
