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

if a>=k:

    print(k)

else:

    if a+b>=k:

        print(a)

    else:

        if a+b+c>=k:

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

if a>=k:

    print(k)

elif a+b>=k:

    print(a)

else:

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

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

+elif a + b >= k: +    print(a) -    if a + b >= k: -        print(a) -    else: -        if a + b + c >= k: -            print((a - (k - (a + b)))) +    print((a - (k - (a + b))))