#include <stdio.h>

int main(void){
    int A, B, C, K;
    int sum = 0;

    scanf("%d%d%d%d", &A, &B, &C, &K);

    if(K < A){
        sum = K;
    }else if(K - A < B){
        sum = A;
    }else{
        sum = A - (K - A - B);
    }

    printf("%d\n", sum);

    return 0;
}
