import Foundation

let input_line: String = readLine()!
let split: [String] = input_line.components(separatedBy: " ")

let a = Int(split[0])!
let b = Int(split[1])!
let c = Int(split[2])!
let k = Int(split[3])!

var ans = a

let s = a + b

if k - s > 0 {
    let t = k - s
    ans -= t
}


print(ans)
