import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
//END OF TEMPLATE

void main(){
    size_t a,b,c,k;
    scan(a,b,c,k);
    if(k<=a){
        k.writeln;
    }else if(k<=a+b){
        a.writeln;
    }else{
        (a-(k-a-b)).writeln;
    }   
}
