program main
    implicit none
    integer a,b,c,k,p
    read(*,*) a,b,c,k
    if(a>=k) then
        p=k 
    else if((a+b)>=k) then
        p=a 
    else
        p=a-(k-a-b)
    end if
    1 write(*,*) p
end program main