function main(input) {
  const args = input.split(' ')
  const a = Number(args[0])
  const b = Number(args[1])
  const c = Number(args[2])
  let k = Number(args[3])
  k -= a
  if (k <= 0) {
    console.log(k)
  } else {
    k -= b
    if(k <= 0) {
      console.log(k)
    } else {
      console.log(a - k)
    }
  }

}
main(require('fs').readFileSync('/dev/stdin', 'utf8'))