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

int main(){
    int A, B, C, K, i;
    int ans = 0;

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

    if(K <= A)ans = K;
    else ans = A;
    if(A+B < K)ans -= (K - (A+B));

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

    return 0;
}