'use strict'

function main(input) {
  const inputList = input.split(' ');
  const a = parseInt(inputList[0])
  const b = parseInt(inputList[1])
  const c = parseInt(inputList[2])
  const k = parseInt(inputList[3])

  let result = 0
  if (a > k) {
    result = k
  } else if (a+b > k) {
    result = a
  } else {
    const tmpc = k - (a + b)
    result = a - tmpc
  }
  console.log(result)
}

main(require("fs").readFileSync("/dev/stdin", "utf8"));