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