

def resolve():

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



    ans = 0

    cnt = min(K, A)

    ans += cnt * 1

    K -= cnt

    if K < 1:

        return print(ans)



    cnt = min(K, B)

    ans += cnt * 0

    K -= cnt

    if K < 1:

        return print(ans)



    ans += K * -1



    print(ans)



if __name__ == "__main__":

    resolve()



def resolve():

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



    ans = 0

    used = min(k,a)

    ans += used * 1

    k -= used

    used = min(k, b)

    k -= used

    ans += k*(-1)

    print(ans)





if __name__ == "__main__":

    resolve()
def resolve():
    A, B, C, K = map(int, input().split())
    ans = 0
    cnt = min(K, A)
    ans += cnt * 1
    K -= cnt
    if K < 1:
        return print(ans)
    cnt = min(K, B)
    ans += cnt * 0
    K -= cnt
    if K < 1:
        return print(ans)
    ans += K * -1
    print(ans)


if __name__ == "__main__":
    resolve()

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


if __name__ == "__main__":
    resolve()

-    A, B, C, K = map(int, input().split()) +    a, b, c, k = list(map(int, input().split())) -    cnt = min(K, A) -    ans += cnt * 1 -    K -= cnt -    if K < 1: -        return print(ans) -    cnt = min(K, B) -    ans += cnt * 0 -    K -= cnt -    if K < 1: -        return print(ans) -    ans += K * -1 +    used = min(k, a) +    ans += used * 1 +    k -= used +    used = min(k, b) +    k -= used +    ans += k * (-1)