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


int main(){
    int score=0;
    int A,B,C,K,i;
    scanf("%d %d %d %d",&A,&B,&C,&K);

    if(K<=A+B-C){
        score = A;
    }else{
        score = A;
        while(K-(A+B)+1<C){
            score--;
            C--;
        }     
    }

    printf("%d\n",score);
    return 0;

}
