#include<stdio.h>

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