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

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

      print *, cnt

end program main
