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



if k <= a:

    print((k*1))

elif k <= a + b:

    print((1*a))

elif k <= a + b + c:

    print((1*a+(k-a-b)*-1))

else:

    print((1*1+c*-1))

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



if a >= k:

    print(k)

else:

    print((a-((k-a)-b)))

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

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

-if k <= a: -    print((k * 1)) -elif k <= a + b: -    print((1 * a)) -elif k <= a + b + c: -    print((1 * a + (k - a - b) * -1)) +if a >= k: +    print(k) -    print((1 * 1 + c * -1)) +    print((a - ((k - a) - b)))