const main = (input) => {
  const [A, B, C, K] = input.trim().split(' ').map(v => parseInt(v))
  const arr = [...new Array(A).fill(1), ...new Array(B).fill(0), ...new Array(C).fill(-1)]
  let ans = 0
  for (let i = 0; i < K; i++) {
    ans += arr[i]
  }
  return console.log(ans)
}

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