const main = (input) => {
  let [a, b, c, k] = input.trim().split(' ').map(Number)

  let ans = a
  k -= a
  k -= b

  if (k > 0) ans -= k

  return console.log(ans)

}

process.env.MYTEST
  ? (process.env.MYTEST === 'test' ? test() : main(require('fs').readFileSync('dev/stdin', 'utf8')))
  : main(require('fs').readFileSync('/dev/stdin', 'utf8'))