program main
    implicit none
    integer a, b, c, k
    integer rest, sum
    integer i 
    read(*,*) a, b, c, k

    sum = 0

    if(k >= a) then
        sum = sum + 1*a
        rest = k - a
    else
        sum = sum + 1*k
        rest = 0
    end if

    if(rest >= b) then
        rest = rest - b
    else
        rest = 0
    end if

    sum = sum + rest*(-1)

    write(*,*) sum
    
    
end program