let line = readLine()!
let inputs = line.split(separator: " ")
var A = Int(inputs[0])!
var B = Int(inputs[1])!
var C = Int(inputs[2])!
var K = Int(inputs[3])!

var sum = 0
while K > 0 {
  if A > 0 {
    sum += 1
    A -= 1
  } else if B > 0 {
    B -= 1
  } else {
    sum -= 1 
    C -= 1
  }
  K -= 1
}
print(sum)