program ABC167B
    implicit none
    integer(8)::A,B,C,K
    read*,A,B,C,K
    if(K>=A+B)then
        print'(i0)',A-(K-A-B)
    else if(K>=A)then
        print'(i0)',A
    else
        print'(i0)',K
    end if
end program ABC167B