let input = readLine()!.split(separator: " ").map { Int($0)! }
let A = input[0]
let B = input[1]
let C = input[2]
let K = input[3]

var score = 0
var count = K

score = min(A, count)
count = max(count - A, 0)
count = max(count - B, 0)
score -= count

print(score)
