   program abc167b
      implicit none
      integer(8)::a,b,c
      integer(8)::k,ans=0

         read *, a,b,c,k

         if (a>=k) then
            print *, k
            stop
         else
            ans=ans+a
            k=k-a
            if (b>=k) then
               print *, ans
               stop
            else
               k=k-b
               ans=ans-k
               print *, ans
               stop
            end if
         end if

   end program abc167b