# -*- coding: utf-8 -*-

"""

Created on Sun May 10 20:54:25 2020



@author: Kanaru Sato

"""



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



if a>=k:

    print(k)

else:

    k = k-a

    if b>= k:

        print(a)

    else:

        k = k-b

        print((a-k))
# -*- coding: utf-8 -*-

"""

Created on Fri May 15 00:35:24 2020



@author: Kanaru Sato

"""



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



if k <= a:

    print(k)

elif a<k and k<=a+b:

    print(a)

else:

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

# -*- coding: utf-8 -*-
"""
Created on Sun May 10 20:54:25 2020
@author: Kanaru Sato
"""
a, b, c, k = list(map(int, input().split()))
if a >= k:
    print(k)
else:
    k = k - a
    if b >= k:
        print(a)
    else:
        k = k - b
        print((a - k))

# -*- coding: utf-8 -*-
"""
Created on Fri May 15 00:35:24 2020
@author: Kanaru Sato
"""
a, b, c, k = list(map(int, input().split()))
if k <= a:
    print(k)
elif a < k and k <= a + b:
    print(a)
else:
    print((a - k + (a + b)))

-Created on Sun May 10 20:54:25 2020 +Created on Fri May 15 00:35:24 2020 -if a >= k: +if k <= a: +elif a < k and k <= a + b: +    print(a) -    k = k - a -    if b >= k: -        print(a) -    else: -        k = k - b -        print((a - k)) +    print((a - k + (a + b)))