#include<stdio.h>
#include<math.h>
int main()
{
    long long int a,b,c,k,i,j,m,n,p,cnt,fla;
    scanf("%lld %lld %lld %lld",&a,&b,&c,&k);

    if(k<=a) printf("%lld\n",k);
    else
    {
        m=k-a;
        if(m<=b) printf("%lld\n",a);
        else
        {
            n=m-b;
            printf("%lld\n",a-n);
        }
    }

    return 0;
}
