#include<stdio.h>

int main()
{
    int a,b,c,k;
    scanf("%d %d %d %d",&a,&b,&c,&k);
    if(k<=a)
    printf("%d",k*1);
    else if(k>a&&k<=a+b)
    {
        printf("%d",a*1);
    }
    else if(k>a+b&&k<=a+b+c)
    {
        printf("%d",(a*1)+((k-(a+b))*(-1)));
    }
}
