#include <stdio.h>

int main(void){
  long long int a,b,c,k,max;
  scanf("%lld %lld %lld %lld",&a,&b,&c,&k);
  if(a >= k){
    max = k;
  }else if(a+b >= k){
    max = a;
  }else{
    max = a;
    k -= (a + b);
    max = a - k;
  }
  printf("%lld\n",max);
  return 0;
}