const processData = (lines) => {
  const [a, b, c, k] = lines[0].split(' ').map(x => +x)
  const rest = Math.max(k-(a+b), 0)
  console.log(Math.min(a, k) - rest)
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})
