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



if a >= k:

    print(k)

    exit()



if k-a-b <= 0:

    print(a)

    exit()



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

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



if a >= k:

    print(k)

    exit()

if b >= k-a:

    print(a)

    exit()

if c >= k-a-b:

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

    exit()



print((a-c))

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

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

-if k - a - b <= 0: +if b >= k - a: -print((a - (k - a - b))) +if c >= k - a - b: +    print((a - (k - a - b))) +    exit() +print((a - c))