#include <stdio.h>

int main(int argc, char const *argv[])
{
    long long A, B, C, K, left;
    scanf("%lld %lld %lld %lld", &A, &B, &C, &K);
    left = K;
    if (left <= A)
    {
        printf("%lld\n", K);
        return 0;
    }

    left -= A;
    if (left <= B)
    {
        printf("%lld\n", A);
        return 0;
    }

    left -= B;
    
    printf("%lld\n", A - left);
    return 0;
}
