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])!
var max = 0

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