import Foundation
let arr=readLine()!.split{$0==" "}.map{Int(String($0))!}
var a=arr[0]
var b=arr[1]
var c=arr[2]
var k=arr[3]
var max = 0

if k > a {
    max += a
    k -= a
    if k > b {
        k -= b
        if  k != 0  {
           max -= k
        }
    }
}else{
    max = a
}