Option Explicit On

Module Module1

    Sub Main()

        Dim tmp As String() = Split(Console.ReadLine(), " ")

        Dim A As Integer = CInt(tmp(0))
        Dim B As Integer = CInt(tmp(1))
        Dim C As Integer = CInt(tmp(2))
        Dim K As Integer = CInt(tmp(3))

        Dim ans As Integer

        If A <= K Then
            ans = A
            K -= A
            If B <= K Then
                K -= B
                If C <= K Then
                    ans -= C
                Else
                    ans -= K
                End If
            End If
        Else
            ans = K
        End If

        Console.WriteLine(ans)

    End Sub
End Module
