const main = input => {
  const [A, B, C, K] = input.flat().map(Number)

  if (K <= A) return [[K]]

  if (K <= A + B) return [[A]]

  return [[A - (K - (A + B))]]
}

const wrapper = data => {
  const input = data.split('\n').map(line => line.split(' '))
  const output = main(input)
  console.log(output.map(line => line.join(' ')).join('\n'))
}

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