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