const TEST = `2 1 1 3`
// A B C K

function Main(input) {
  const inputArr = input.trim().split(" ");
  let countA = parseInt(inputArr[0])
  let countB = parseInt(inputArr[1])
  let countC = parseInt(inputArr[2])
  let countK = parseInt(inputArr[3])
  let res = 0
  for (let i = countK; i > 0; i--) {
    if (countA !== 0) {
      countA = countA - 1
      res = res + 1
    } else if (countB !== 0) {
      countB = countB - 1
    } else {
      countC = countC - 1
      res = res - 1
    }
  }
  console.log(res)
}

//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));