#!/usr/bin/env python3

import sys





def solve(A: int, B: int, C: int, K: int):

    sum = 0

    l = K - A

    if l <= 0:

        sum += K

    else:

        sum += A

    l -= B

    if l > 0:

        sum -= l

    print(sum)

        

        

    return





# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    A = int(next(tokens))  # type: int

    B = int(next(tokens))  # type: int

    C = int(next(tokens))  # type: int

    K = int(next(tokens))  # type: int

    solve(A, B, C, K)



if __name__ == '__main__':

    main()

#!/usr/bin/env python3

import sys



# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

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

    score = min(K, A) * 1

    K -= A + B

    if K > 0:

        score -= K

    print(score)

if __name__ == '__main__':

    main()

#!/usr/bin/env python3
import sys


def solve(A: int, B: int, C: int, K: int):
    sum = 0
    l = K - A
    if l <= 0:
        sum += K
    else:
        sum += A
    l -= B
    if l > 0:
        sum -= l
    print(sum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(A, B, C, K)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    A, B, C, K = list(map(int, input().split()))
    score = min(K, A) * 1
    K -= A + B
    if K > 0:
        score -= K
    print(score)


if __name__ == "__main__":
    main()

- -def solve(A: int, B: int, C: int, K: int): -    sum = 0 -    l = K - A -    if l <= 0: -        sum += K -    else: -        sum += A -    l -= B -    if l > 0: -        sum -= l -    print(sum) -    return - - -    def iterate_tokens(): -        for line in sys.stdin: -            for word in line.split(): -                yield word - -    tokens = iterate_tokens() -    A = int(next(tokens))  # type: int -    B = int(next(tokens))  # type: int -    C = int(next(tokens))  # type: int -    K = int(next(tokens))  # type: int -    solve(A, B, C, K) +    A, B, C, K = list(map(int, input().split())) +    score = min(K, A) * 1 +    K -= A + B +    if K > 0: +        score -= K +    print(score)