program main
integer :: a,b,c,k,sum = 0

read(*,*)a,b,c,k

do i= 1,k
if (a>0) then
sum = sum + 1
a = a - 1
cycle
else if (b>0) then
b = b - 1
cycle
else if (c>0) then
sum = sum - 1
c = c - 1
cycle
end if
end do

write(*,*) sum
end program main