program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b,c,k

    read*, a,b,c,k

    if (a+b >= k) then
        print'(i0)', a
    else
        print'(i0)', a-min(k-(a+b), c)
    end if
    
end program main