func main() {
    let abck = readLine()!.split(separator: " ").map {Int($0)!}
    let (A, B, C, K) = (abck[0], abck[1], abck[2], abck[3])
    // a + b + c = k, a > b > c
    var total: Int
    total = min(K, A) * 1
    if K > A {
        total += min(K-A, B) * 0
        if K - A > B {
            total += (K - A - B) * -1
        }
    }
    print(total)
}

main()
