# B - Easy Linear Programming



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

if k <= a:

    print(k)

elif a < k <= a + b:

    print(a)

else:

    print((a - (k - (a + b))))

# B - Easy Linear Programming



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

print((min(k, a, a + a + b - k)))

# B - Easy Linear Programming
a, b, c, k = list(map(int, input().split()))
if k <= a:
    print(k)
elif a < k <= a + b:
    print(a)
else:
    print((a - (k - (a + b))))

# B - Easy Linear Programming
a, b, c, k = list(map(int, input().split()))
print((min(k, a, a + a + b - k)))

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