program main
  integer(8) :: A,B,C,K
  integer(8) :: i,j
  read(*,*) A,B,C,K
  if ( K-A > 0) then
     if (K-A-B > 0 ) then
        write(*,'(i0)') A-(K-A-B)
     else
        write(*,'(i0)') A
     end if
  else 
     write(*,'(i0)')  K
  end if
end program main
