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

sum = A+B

if(sum>=K):

    if(A>K):

        print(K)

    else:

        print((A*1+B*0))

elif (sum<K):

    print((A*1+B*0+(K-(A+B))*-1))
A,B,C,K=list(map(int,input().split(" ")))



xa = min(A,K)

K -= xa

xb = min(B,K)

K -= xb

xc = min(C,K)



print((xa-xc))
A, B, C, K = list(map(int, input().split(" ")))
sum = A + B
if sum >= K:
    if A > K:
        print(K)
    else:
        print((A * 1 + B * 0))
elif sum < K:
    print((A * 1 + B * 0 + (K - (A + B)) * -1))

A, B, C, K = list(map(int, input().split(" ")))
xa = min(A, K)
K -= xa
xb = min(B, K)
K -= xb
xc = min(C, K)
print((xa - xc))

-sum = A + B -if sum >= K: -    if A > K: -        print(K) -    else: -        print((A * 1 + B * 0)) -elif sum < K: -    print((A * 1 + B * 0 + (K - (A + B)) * -1)) +xa = min(A, K) +K -= xa +xb = min(B, K) +K -= xb +xc = min(C, K) +print((xa - xc))