'use strict'

const Main = (input) => {
  const tmp = input.trim().split(' ')
  const A = parseInt(tmp[0])
  const B = parseInt(tmp[1])
  const C = parseInt(tmp[2])
  let K = parseInt(tmp[3])

  let ans = 0

  if (K - A >= 0) {
    K = K - A
    ans += 1 * A
  } else if (K - A < 0) {
    ans += 1 * K
    K = 0
  }

  if (K != 0 && (K - B) >= 0) {
    K = K - B
  } else if (K != 0 && (K - B) < 0) {
    K = 0
  }
console.log(K)
  if (K != 0 && (K - C) >= 0) {
    K = K - C
    ans -= (1 * C)
  } else if (K != 0 && (K - C) < 0) {
    ans -= (1 * K)
  }

  let result = ans

  console.log(result)
  return result
}

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