#include <stdio.h>

int main(void)
{
        int k;
        long a,b,c,s;

        scanf("%ld%ld%ld%d",a,b,c,k);

        if(k<=a) s=a;
        else {
                if(k<=b) s=a;
                else s=a+(k-a-b)*(-1);
        }

        printf("%ld\n",s);
        return 0;
}
