#include <stdio.h>

int main(){
  int A,B,C,K;
  scanf("%d %d %d %d",&A,&B,&C,&K);
  
  int max = 0;
  
  if(K <= A){
    max = K;
  }else if(K < A+B){
    max = A;
  }else{
    max = A - (K - A - B);
  }
  printf("%d",max);
  
  return 0;
}