func solve(_ A: Int, _ B: Int, _ C: Int, _ K: Int) -> Int {
  
  var K = K
  if K <= A {
    return K
  }
  K -= A
  
  if K <= B {
    return A
  }
  
  K -= B
  
  return A - K
  
}

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])!

print(solve(A, B, C, K))