import Foundation

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

var one = Int(split[0])!
var zero = Int(split[1])!
var minus = Int(split[2])!
var count = Int(split[3])!

//var one = 1
//var zero = 2
//var minus = 3
//
//var count = 4

var max = 0

while count > 0 {
    
    if one > 0 {
        max += 1
        one -= 1
    } else if zero > 0 {
        zero -= 1
    } else if minus > 0 {
        max -= 1
        minus -= 1
    }
    
    count = count - 1
}

print(max)

