const main = (input) => {
  const [A, B, C, K] = input.trim().split(' ').map(v => parseInt(v))
  let ans = 0
  ans += A
  if (A >= K || A + B >= K) return console.log(ans)

  ans -= (K - (A+B))
  return console.log(ans)
}

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