import Foundation

func main() {
    let inputString = readLine()!
    let split: [Int] = inputString.components(separatedBy: " ").map{Int(String($0))!}
    
    let oneNum = split[0]
    let zeroNum = split[1]
    var cardNum = split[3]
    var res = 0
    
    guard (oneNum < cardNum) else {
        print(cardNum)
        return
    }
        res += oneNum
        cardNum -= oneNum
    
    guard (zeroNum < cardNum) else {
        print(res)
        return
    }
        cardNum -= zeroNum
    
    print(res - cardNum)
}

main()
