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 = 0

if k > a {
ans = a

let s = a + b
let t = k - s

if t > 0 {
    ans -= t
}

} else {
  ans = k
}

print(ans)
