program main
      implicit none
      integer(8) :: a, b, c, k, cnt

      read *, a, b, c, k
     
      if ( k >= (a+b) ) then
        cnt = a - ( k-a-b )
      else
        cnt = a
      end if

      print *, cnt
end program main
