import Foundation

let input = readLine()!
let inputArray = input.components(separatedBy: " ")
let A = Int(inputArray[0])!
let B = Int(inputArray[1])!
let C = Int(inputArray[2])!
let K = Int(inputArray[3])!

if A + B > K {
    print(K)
} else {
    var remain = K - A - B
    print(A - remain)
}
