Imports System

Module Program
    Sub Main(args As String())

        Dim ABCK() As Integer = Console.ReadLine().Split(" ").Select(Function(x) Integer.Parse(x)).ToArray()

        Dim wk As Integer = ABCK(3)
        Dim ans As Integer = 0
        If ABCK(0) <= wk Then
            ans += ABCK(0)
            wk -= ABCK(0)
            wk -= ABCK(1)
            If 0 < wk Then
                If ABCK(2) <= wk Then
                    ans -= ABCK(2)
                Else
                    ans -= wk
                End If
            End If
        Else
            ans += wk
        End If

        Console.WriteLine(ans)

    End Sub
End Module
