import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10 ** 7)



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

if a + b >= k:

    print((min(a, k)))

else:

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

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10 ** 7)



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

print((min(a, k) if a + b >= k else 2 * a + b - k))

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
a, b, c, k = list(map(int, readline().split()))
if a + b >= k:
    print((min(a, k)))
else:
    print((a - (k - a - b)))

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
a, b, c, k = list(map(int, read().split()))
print((min(a, k) if a + b >= k else 2 * a + b - k))

-a, b, c, k = list(map(int, readline().split())) -if a + b >= k: -    print((min(a, k))) -else: -    print((a - (k - a - b))) +a, b, c, k = list(map(int, read().split())) +print((min(a, k) if a + b >= k else 2 * a + b - k))