#Solver:Rute

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

if k<=a:

  print(k)

elif a<k<a+b:

  print(k)

else:

  #-1のカードの枚数超過 (k-(a+b))枚

  print((a-(k-(a+b))))
#Solver:Rute

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

if 0<=k<=a:

  print(k)

elif a<k<=a+b:

  print(a)

else:

  print((a-(k-(a+b))))
# Solver:Rute
a, b, c, k = list(map(int, input().split()))
if k <= a:
    print(k)
elif a < k < a + b:
    print(k)
else:
    # -1のカードの枚数超過 (k-(a+b))枚
    print((a - (k - (a + b))))

# Solver:Rute
a, b, c, k = list(map(int, input().split()))
if 0 <= k <= a:
    print(k)
elif a < k <= a + b:
    print(a)
else:
    print((a - (k - (a + b))))

-if k <= a: +if 0 <= k <= a: -elif a < k < a + b: -    print(k) +elif a < k <= a + b: +    print(a) -    # -1のカードの枚数超過 (k-(a+b))枚