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

if a>=k:

  print(k)

elif a<k:

  if k-a<=b:

    print(a)

  elif k-a >b:

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

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

ans=0

ans=min(k,a)

k-=min(k,a)

ans+=min(k,b)*0

k-=min(k,b)

ans+=min(k,c)*(-1)

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

a, b, c, k = list(map(int, input().split()))
ans = 0
ans = min(k, a)
k -= min(k, a)
ans += min(k, b) * 0
k -= min(k, b)
ans += min(k, c) * (-1)
print(ans)

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