#include<stdio.h>
int MIN(int a,int b){return a<b?a:b;}
int main(){
  int a,b,c,n,s=0,m;
  scanf("%d %d %d %d",&a,&b,&c,&n);
  m=MIN(a,n);
  s+=m;
  n-=m;
  m=MIN(b,n);
  n-=m;
  m=MIN(c,n);
  s-=m;
  printf("%d\n",s);
  return 0;
}
