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

count = 0



if a <= k:

    count += a

else:

    count += k

    print(count)

    exit()

k -= a



if b <= k:

    k -= b

else:

    print(count)

    exit()





count -= k



print(count)
A,B,C,K=list(map(int,input().split()))

if (A+B)>=K and A<=K:

    print(A)

elif (A+B)>=K and A>=K:

    print(K)

elif (A+B)<K:

    print((A-(K-A-B)))
a, b, c, k = list(map(int, input().split()))
count = 0
if a <= k:
    count += a
else:
    count += k
    print(count)
    exit()
k -= a
if b <= k:
    k -= b
else:
    print(count)
    exit()
count -= k
print(count)

A, B, C, K = list(map(int, input().split()))
if (A + B) >= K and A <= K:
    print(A)
elif (A + B) >= K and A >= K:
    print(K)
elif (A + B) < K:
    print((A - (K - A - B)))

-a, b, c, k = list(map(int, input().split())) -count = 0 -if a <= k: -    count += a -else: -    count += k -    print(count) -    exit() -k -= a -if b <= k: -    k -= b -else: -    print(count) -    exit() -count -= k -print(count) +A, B, C, K = list(map(int, input().split())) +if (A + B) >= K and A <= K: +    print(A) +elif (A + B) >= K and A >= K: +    print(K) +elif (A + B) < K: +    print((A - (K - A - B)))