import sys

def main():

    a,b,c,k=tuple(map(int,sys.stdin.readline().split()))

    if k<=a:

        print(k)

    else:

        if k<=a+b:

            print(a)

        else:

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

if __name__=='__main__':main()
import sys

def main():

    a,b,c,k=tuple(map(int,sys.stdin.readline().split()))

    print(k) if k<=a else print(a) if k<=a+b else print(2*a+b-k)

if __name__=='__main__':main()

import sys


def main():
    a, b, c, k = tuple(map(int, sys.stdin.readline().split()))
    if k <= a:
        print(k)
    else:
        if k <= a + b:
            print(a)
        else:
            print((2 * a + b - k))


if __name__ == "__main__":
    main()

import sys


def main():
    a, b, c, k = tuple(map(int, sys.stdin.readline().split()))
    print(k) if k <= a else print(a) if k <= a + b else print(2 * a + b - k)


if __name__ == "__main__":
    main()

-    if k <= a: -        print(k) -    else: -        if k <= a + b: -            print(a) -        else: -            print((2 * a + b - k)) +    print(k) if k <= a else print(a) if k <= a + b else print(2 * a + b - k)