#include<stdio.h>
#include<stdlib.h>

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