import itertools

import math

import fractions

import functools



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



sum = 0



if k <= a:

    sum = k

    print(sum)

    quit()

elif k > a:

    sum += a

    k = k - a



if k <= b:

    print(sum)

    quit()

elif k > b:

    k = k - b

    sum -= k * 1



print(sum)

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()))
sum = 0
if k <= a:
    sum = k
    print(sum)
    quit()
elif k > a:
    sum += a
    k = k - a
if k <= b:
    print(sum)
    quit()
elif k > b:
    k = k - b
    sum -= k * 1
print(sum)

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))

-    sum = k -    print(sum) +    print(k) -elif k > a: -    sum += a -    k = k - a +else: +    sum = a +    k -= a -elif k > b: -    k = k - b -    sum -= k * 1 -print(sum) +else: +    k -= b +print((sum - k))