#include<stdio.h>
int main()
{
    int a,b,c,k;
    scanf("%d%d%d%d",&a,&b,&c,&k);
    int s=0;
    k=k-a;
    s=s+a;
    if(k!=0){k=k-b;
        if(k!=0){while(k!=0){
                k=k-1;
                s=s-1;
                
            }}}
    printf("%d",s);
    return 0;
}