Class Main
    Public Shared Sub Main()
    Dim NM() As string = Split(Console.ReadLine())
    dim A as long = Long.Parse(NM(0))
    dim B as long = clng(NM(1))
    dim C as long = clng(NM(2))
    dim K as long = clng(NM(3))
    dim i1,i2 as long
    if K > A then
      i1 = A
      i2 = K - A
      if i2 > B then
        i2 = i2 - B
        i1 = i1 - i2
      end if  
    else
      i1 = K
    end if  
      Console.WriteLine(i1)
   End Sub
End Class