def main():



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

    v = 0

    if K <= A:

        return K

    elif K <= A+B:

        return A

    else:

        return A - (K-A-B)



if __name__ == '__main__':

    print((main()))

def main():



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

    if K <= A:

        return K

    elif K <= A+B:

        return A

    else:

        return A - (K-A-B)





if __name__ == '__main__':

    print((main()))

def main():
    A, B, C, K = list(map(int, input().split()))
    v = 0
    if K <= A:
        return K
    elif K <= A + B:
        return A
    else:
        return A - (K - A - B)


if __name__ == "__main__":
    print((main()))

def main():
    A, B, C, K = list(map(int, input().split()))
    if K <= A:
        return K
    elif K <= A + B:
        return A
    else:
        return A - (K - A - B)


if __name__ == "__main__":
    print((main()))

-    v = 0