import itertools

import math

import fractions

import functools



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

sum = 0

if k <= a:

    print(k)

    quit()

else:

    sum = a

    k -= a

if k <= b:

    print(sum)

    quit()

else:

    k -= b

print((sum-k))

import itertools

import math

import fractions

import functools



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

if k <= a:

    print(k)

elif k <= a+b:

    print(a)

else: print((a-(k-a-b)))
import itertools
import math
import fractions
import functools

a, b, c, k = list(map(int, input().split()))
sum = 0
if k <= a:
    print(k)
    quit()
else:
    sum = a
    k -= a
if k <= b:
    print(sum)
    quit()
else:
    k -= b
print((sum - k))

import itertools
import math
import fractions
import functools

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

-sum = 0 -    quit() +elif k <= a + b: +    print(a) -    sum = a -    k -= a -if k <= b: -    print(sum) -    quit() -else: -    k -= b -print((sum - k)) +    print((a - (k - a - b)))