function main(input) {
  let [A, B, C, K] = input.split(' ').map(Number)
  let v = 0
  K = K - A
  if (K <= 0) {
    v += (K + A)
    console.log(v)
    return
  } else {
    v += A
  }
  // console.log(v, K)
  K = K - B
  if (K <= 0) {
    console.log(v)
    return
  }
  // console.log(v, K)
  K = K - C
  if (K <= 0) {
    v -= (K + C)
    console.log(v)
  } else {
    v -= C
    console.log(v)
  }
}

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