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

    read*, a,b,c,k

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