#include <stdio.h>
int main(void){
    long int a,b,c,k,sum=0;
    
    scanf("%ld",&a);
    scanf("%ld",&b);
    scanf("%ld",&c);
    scanf("%ld",&k);
    
    if(k<=a){
        sum=k;
    }else if(k<=(a+b)){
        sum=a;
    }else{
        c=k-a-b;
        sum=a-c;
    }
    
    printf("%ld\n",sum);
    
    return 0;
}