#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"ctype.h"
int main()
{
    long long a,b,c,k;
    scanf("%lld %lld %lld %lld",&a,&b,&c,&k);
    if(a<=k)
    {
        if(b<=k-a)
        {
            printf("%lld",a-k+a+b);
        }
        else
            printf("%lld",a);
    }
    else
        printf("%lld",k);

    return 0;
}
