var input = require('fs').readFileSync('/dev/stdin', 'utf8').split('\n')
var [A, B, C, K] = input[0].split(' ').map(Number)
console.log(solver())

function solver() {
  if (K <= A) {
    return K
  }
  K -= A
  if (K <= B) {
    return A
  }
  K -= B
  return A - K
}
