extension Collection {
    func mapToTuple() -> (Element, Element, Element, Element) {
        var iterator = makeIterator()
        return (iterator.next()!, iterator.next()!, iterator.next()!, iterator.next()!)
    }
}
var (A, B, C, K) = readLine()!.split(separator: " ").map { Int($0)! }.mapToTuple()

var result = 0

if 0 < K {
    result += min(A, K)
    K -= A
}
if 0 < K {
    K -= B
}
if 0 < K {
    result -= min(C, K)
    K -= C
}

print(result)
