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

for _ in 0..<k {
    if k == a {
        max = a
        break
    }
    if k == b {
           max = 0
           break
       }
    if k == c {
           max -= c
           break
       }
    if a != 0 {
        max+=1
        a-=1
    }else if b != 0 {
        max+=0
        b-=1
    }else if c != 0{
        max -= 1
        c -= 1
    }
}
print(max)