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

let line = readArrayInt()
let (A, B, C, K) = (line[0], line[1], line[2], line[3])

if K <= A {
    print(K)
} else if K <= A + B {
    print(A)
} else {
    print(A - (K - A - B))
}
