#include<stdio.h>

int main(void){
    int A, B, C, K,score = 0;;
    scanf("%d %d %d %d",&A, &B, &C, &K);
    if(A >= K){
        score = K;
    }
    else{
        score = A;
        K -= A;
        if(B < K){
            score = score - (C-(A+K-B));
        }
    }
    printf("%d\n",score);
    return 0;
}