#include <stdio.h>

int main(){
  int a,b,c,k,d=0;
  scanf("%d %d %d %d",&a, &b, &c, &k);
  
  if (a >= k)
    d = k;
  else if (a + b >= k)
    d = a;
  else
    d = 2*a + b - k;
  
  printf("%d",d);
  
  return 0;
}