import Foundation

func readInts() -> [Int]{
    return readLine()!.split(separator: " ").map { Int($0)! }
}

func main(){
    let abck = readInts()
    let (a,b,_,k) = (abck[0],abck[1],abck[2],abck[3])
        
    if k <= a{
        print(k)
        return
    }
    
    var ans = a
    
    if k - a - b > 0 {
        ans -= (k - a - b)*1
    }
    print(ans)
}
    
main()
