program bm
implicit none
integer::A,B,C,K
integer::ans=0
integer::cnt
read*,A,B,C,K
ans=ans+min(A,K)
K=K-A
if(K>0)then
    K=K-B
    if(K>0)then
        ans=ans-min(C,K)
    endif
endif
print"(i0)",ans
end program bm