a, b, c, k = list(map(int, input().split(' ')))

if a+b>k:

    print(k)

else:

    if a+b+c<=k:

        print((a-c))

    else:

        print((a-(k-(a+b))))
a, b, c, k = list(map(int, input().split(' ')))

if a+b>k:

    print(k)

elif a+b+c<=k:

    print((a-c))

else:

    print((a-(k-(a+b))))
a, b, c, k = list(map(int, input().split(" ")))
if a + b > k:
    print(k)
else:
    if a + b + c <= k:
        print((a - c))
    else:
        print((a - (k - (a + b))))

a, b, c, k = list(map(int, input().split(" ")))
if a + b > k:
    print(k)
elif a + b + c <= k:
    print((a - c))
else:
    print((a - (k - (a + b))))

+elif a + b + c <= k: +    print((a - c)) -    if a + b + c <= k: -        print((a - c)) -    else: -        print((a - (k - (a + b)))) +    print((a - (k - (a + b))))