import sys

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

N = 0

if K - A < 0:

    print(K)

    sys.exit()

else:

    N = A

    K = K - A

if K - B < 0:

    print(N)

    sys.exit()

else:

    K = K - B

print((N - K))
import sys

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

c = 0

if K <= A:

    c += K

    print(c)

    sys.exit()

else:

    c += A

    K -= A

if K <= B:

    print(c)

    sys.exit()

else:

    K -= B

    c -= K

    print(c)
import sys

A, B, C, K = list(map(int, input().split()))
N = 0
if K - A < 0:
    print(K)
    sys.exit()
else:
    N = A
    K = K - A
if K - B < 0:
    print(N)
    sys.exit()
else:
    K = K - B
print((N - K))

import sys

A, B, C, K = list(map(int, input().split()))
c = 0
if K <= A:
    c += K
    print(c)
    sys.exit()
else:
    c += A
    K -= A
if K <= B:
    print(c)
    sys.exit()
else:
    K -= B
    c -= K
    print(c)

-N = 0 -if K - A < 0: -    print(K) +c = 0 +if K <= A: +    c += K +    print(c) -    N = A -    K = K - A -if K - B < 0: -    print(N) +    c += A +    K -= A +if K <= B: +    print(c) -    K = K - B -print((N - K)) +    K -= B +    c -= K +    print(c)